简体   繁体   English

Excel VBA检查工作簿是否已打开

[英]Excel vba to check if a workbook has opened

IS there any way to check if an excel file is opened or not? 有什么方法可以检查是否打开了excel文件? 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 .................. 此人寻找一个名为Phone.xls或Phone.xlsx或.........的打开的工作簿

If found the workbook is closed. 如果找到,则关闭工作簿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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