Macro to see if a file is open

StewartS

Board Regular
Joined
Feb 24, 2002
Messages
217
I have a macro which opens a specific file. I would like to check to see if that file is open so that there is no error on running the macro.

I would like something along the lines of

If workbook x is open then
activate workbook X else
open workbook x
end if

Thanks

Stewart
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Something like this would work. I don't know if there's anything more efficient though.

<pre>
Dim oWkBook As Workbook
Dim bCheck As Boolean

bCheck = False

For Each oWkBook In Workbooks
If UCase(oWkBook.FullName) = UCase("C:TEMPBook1.xls") Then
bCheck = True
Exit For
End If
Next

If bCheck Then
'Your code
MsgBox "hi"
End If</pre>

HTH
 
Upvote 0
This one works too. I like the other version as you can use a variable.

If 'some code
Workbooks.Open FileName:= _
"\serverfoldersheet.xls", _
updatelinks:=3, WriteResPassword:="pw"
'check to see if read only
If ActiveWorkbook.ReadOnly Then
ActiveWorkbook.Saved = True
ActiveWorkbook.Close
end if
 
Upvote 0

Forum statistics

Threads
1,213,524
Messages
6,114,117
Members
448,549
Latest member
brianhfield

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