check if a file is already open in VBA,

small

New Member
Joined
May 25, 2004
Messages
26
How can i check if a file is already open in VBA, i get a run time error because the file is already open and it cant open the file of same name although it is in a different directory , so i just want to have a function that return a boolean if already open so that i can just activate the file instead trying to open from the second location
Its 100 am i still need to sleep from yesterday so any urgent help would be very much apprectaited
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Many ways to do this, here's one possibility, modify for path and file name:

Sub Test2()
On Error Resume Next
Workbooks("YourFileName").Activate
If Err <> 0 Then Err.Clear: Workbooks.Open ("C:\Your\File\Path\YourFileName.xls")
End Sub
 
Upvote 0
i dont understand how this works or is going to work
if it has an error it ll just move to next.
why do i activate a workbook first then if error try opening it?
could you put some quick short comments to explain the code so that i can understand if it is what i need
thanks
 
Upvote 0
Instead of sitting there with this "urgent" matter wondering out loud about the code while your clock is ticking, why don't you just test it to see for yourself if it's what you need? It works fine based on what you said you want, which is, it activates your workbook if it is open, and if it is not open it opens it from the file path.
 
Upvote 0
would this work on a similar bases to what u supplied ealier .....caus i now have a situation where the file has been closed but i'm trying to save it. I need to accomadate for this situation that if the file is closed then i need to proceed, in my main func i have on error goto a and an a: label, but it doesnt seem to work, any other ways
thanks for before it did work.


Sub SaveIfFileOpen(Filename As String)
On Error Resume Next
Workbooks(Filename).Activate
ActiveWorkbook.SaveAs Filename:= _
"D:\Projects\myproj\amy\extFiles\mrpst\dupIDFail\" & ActiveWorkbook.Name _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

If Err <> 0 Then Err.Clear: Exit Sub
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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