简体   繁体   中英

Excel vba to check if a workbook has opened

IS there any way to check if an excel file is opened or not? If it is opened, then how can i close it?

thanks in advance

If the workbook name does not include the period character, it can be as simple as:

Sub TestForOpen()
    Dim wb As Workbook, st As String
    st = "Phone"
    For Each wb In Workbooks
        stwb = Split(wb.Name, ".")(0)
        If st = stwb Then
            wb.Activate
            ActiveWorkbook.Close
            Exit Sub
        End If
    Next wb
End Sub

This one looks for an open workbook named Phone.xls or Phone.xlsx or ..................

If found the workbook is closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM