Test if a Specific workbook is open

cprince

Board Regular
Joined
Jul 4, 2006
Messages
67
Hi All,

Can someone provide me with some code that tests if a certain workbook is open, If it s open 'do something' if not, Msgbox "File not open..."
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
this code will test if the file name TEST.XLS is open or not;
Code:
Sub test_if_open()
   Dim File As String
    File = "TEST.XLS"
        If IsWbOpen(File) Then
            MsgBox "Open"
                Else
                    MsgBox "File Not Open"
        End If
End Sub
Function IsWbOpen(wbName As String) As Boolean
    On Error Resume Next
    IsWbOpen = Len(Workbooks(wbName).Name)
End Function
 
Upvote 0
hi c.

here is an example which should help.

cheers. ben.

Code:
Sub GetWorkbook()

    Dim wbOpen As Workbook, strWorkbook As String

'   Suppress errors
    On Error Resume Next
    
    strWorkbook = "Book1" '<- change this to your workbook name
    Set wbOpen = Workbooks(strWorkbook)
    
'   Test if workbook is open
    If wbOpen Is Nothing Then
'       Workbook is not open
        MsgBox "Can't find it, sorry!"
    Else
'       Workbook is open
        MsgBox "I got it!  Here you go!"
        wbOpen.Activate
    End If
    
    On Error GoTo 0
    Set wbOpen = Nothing
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,217,476
Messages
6,136,876
Members
450,029
Latest member
MissQuotation

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top