VBA check if workbook open

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,368
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I found some code and with a slight modification I'm not getting the right results.

I have workbook names in A1:A8 in my master workbook. If the workbook with the name in A1:A8 is open, I would like B1:B8 to show the corresponding "Open" or "Closed".

Any thoughts on how to achieve this?

Code:
Sub IsWorkBookOpen()
    Dim wBook As Workbook
    Dim i As Long
    On Error Resume Next
    For i = 1 To 8
        Set wBook = Workbooks(Cells(i, 1))
        If wBook Is Nothing Then
            Cells(i, 2).Value = "Open" 'Not open
        Else
            Cells(i, 2).Value = "Open" 'Open
        End If
    Next i
End Sub
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this UDF

Code:
Function IsWorkbookOpen(wbName as String) As Boolean
    On Error Resume Next
    IsWorkbookOpen = (LCase(Workbooks(wbName).Name) = LCase(wbName))
    On Error Goto 0
End Function
 
Upvote 0
Hi,
try this update to your code


Code:
Sub IsWorkBookOpen()
    Dim wBook As Workbook
    Dim i As Long
    On Error Resume Next
    For i = 1 To 8
        Set wBook = Workbooks(Cells(i, 1).Text)
            Cells(i, 2).Value = IIf(wBook Is Nothing, "Closed", "Open")
        Set wBook = Nothing
        Err.Clear
    Next i
End Sub

Dave
 
Upvote 0
Thank you. Both of these worked great for my needs.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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