Checking If Workbook already open

JimM

Well-known Member
Joined
Nov 11, 2003
Messages
741
I'm trying to write a macro, part of which needs to check if a workbook is already open and if not then open it.
I've grabbed this bit of code from the internet but it's not working as I'd expect as it tries to open the workbook no matter what

Any ideas what might be wrong

On Error Resume Next
Set wb = Workbooks("S:\Gainsborough\Shared Data\Engineering General\DowntimeSheet v2.xlsx")
If Err <> 0 Then 'workbook isn't open
Workbooks.Open Filename:= _
"S:\Gainsborough\Shared Data\Engineering General\DowntimeSheet v2.xlsx", _
UpdateLinks:=0
Windows("Engineer Time.xlsm").Activate
End If
On Error GoTo 0
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
See if this works

VBA Code:
Dim wbName As String, wb As Workbook
wbName = "DowntimeSheet v2.xlsx"
On Error Resume Next
Set wb = Workbooks(wbName)
If wb.Name = wbName then
    ' file is open'
Else
    Workbooks.Open Filename:= _
        "S:\Gainsborough\Shared Data\Engineering General\DowntimeSheet v2.xlsx", _
    UpdateLinks:=0
    Windows("Engineer Time.xlsm").Activate
End If
On Error Goto 0
 
Upvote 0
Thanks @jasonb75

You pointed me in the right direction - once I tweaked my code to remove the path from the filename it worked!
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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