Push Alt+F11 and go to Insert>Module and paste in this code
Function Workbook_Open(WbookName As String) As Boolean
Dim wBookCheck As Workbook
Application.Volatile
On Error Resume Next
Set wBookCheck = Workbooks(WbookName)
Workbook_Open = Not wBookCheck Is Nothing
On Error GoTo 0
End Function
Sub IsWorkbook_Open()
Dim wBookCheck As Workbook
Application.Volatile
On Error Resume Next
Set wBookCheck = Workbooks("Data.xls")
myvar = Not wBookCheck Is Nothing
MsgBox myvar
On Error GoTo 0
End Sub
Here is one I use, from a wb name typed in a cell, to open if closed or
activate if open
Sub GetWorkbook()
If ActiveCell.Value = "" Then Exit Sub
workbookname = ActiveCell.Value
On Error GoTo OpenWorkbook
Windows(workbookname & ".xls").Activate
Exit Sub
OpenWorkbook:
Workbooks.Open(workbookname & ".xls").RunAutoMacros xlAutoOpen
End Sub
And one from Dave
Sub GetWorkbookA() 'Dave Hawley
Dim wBook As Workbook
On Error Resume Next
Set wBook = Workbooks("Book1.xls")
If wBook Is Nothing Then
Workbooks.Open ' <File and path>
Else
wBook.Activate
End If
On Error GoTo 0
End Sub
Function IsFileOpen(myfilename As String) As Boolean
Dim wb As Workbook
IsFileOpen = False
For Each wb In Application.Workbooks
If wb.Name = myfilename Then
IsFileOpen = True
Exit For
End If
Next wb
End Function
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.