Excel doesnt recognize workbook as open

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
Hi, I am using excel 2003 and I'm trying to write a macro that checks to see if a file is open, and if not then it will open it.

I've found lots of different ways of doing this. But none of them are working for me.

So i tried this:
Code:
Function IsWorkbookOpen(WorkbookName As String) As Boolean
Dim wb As Workbook
For Each wb In Excel.Workbooks
If UCase$(wb.Name) = UCase$(WorkbookName) Then
IsWorkbookOpen = True
Exit Function
End If
Next
End Function
Sub test()
MsgBox IsWorkbookOpen("X:\directory\sub directory\MY FILE.xls")
End Sub

And even when the workbook is open, it still returns "FALSE" in the message box.

I cant for the life of me figure out why. Its very frustrating. Can anyone offer any ideas on what the problem might be?

The only thing I can think of that might be causing issues, is that the workbook is a shared workbook. Now I dont know why that would matter, but every little bit of info counts...right?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You should not be including the file path:
Code:
Sub test()
MsgBox IsWorkbookOpen("MY FILE.xls")
End Sub

If you want to include the file path, then you need to test wb.Fullname in the function.
 
Upvote 0
Try this alternative

Code:
Function IsWorkbookOpen(WorkbookName As String) As Boolean
On Error Resume Next
IsWorkbookOpen = Len(Workbooks(WorkbookName).Name)
End Function
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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