Sulprobil
Search…
IsOpen
Check whether a file is open
Please read my Disclaimer.
1
Function IsOpen(sBasename As String) As Boolean
2
Dim wb As Workbook
3
On Error Resume Next
4
Set wb = Workbooks(sBasename)
5
On Error GoTo 0
6
IsOpen = Not wb Is Nothing
7
End Function
Copied!
This Function only detects open files within the Excel instance it is running. But when a subsequent Open fails, you can return an error and stop - the file was maybe open in another Excel instance or by another network user.
Test it:
1
Sub Test_Open()
2
Dim wb As Workbook
3
Debug.Print IsOpen(ThisWorkbook.Name)
4
Debug.Print IsOpen("This_File_does_not_even_exist.xlsx")
5
End Sub
Copied!
IsOpen.xlsm
14KB
Binary
IsOpen.xlsm
Last modified 8mo ago
Copy link