Check if files exist or are open

Westoner

New Member
Joined
Jan 4, 2011
Messages
23
A number fo my macros fail during running, because the folder I am trying to create already exists or the file I am trying to open is already open.

Are there simple If/Then statements which would prevent the macro stopping in both cases?

Westoner
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
When you say "file", do you mean "workbook"?

Check for the existence of a directory as follows:-
Code:
If Dir(sFolder, vbDirectory)="" Then [COLOR=green]' doesn't exist
[/COLOR]
 
Upvote 0
Simple one to test if file is open:
Code:
If Not (Windows("my file.xls") Is Nothing) Then
   msgbox "File: ""my file.xls"" is open, macro stopping"
   Exit Sub
End If
 
Upvote 0
I have a command:

MkDir "C:\Test\" & Range("C2").Value & Range("D2").Value

I would like to alter the coding so that if the directory already exists it will open it instead.

Similarly, I have a command:

Workbooks.Open fileName:= "C\Test\" & Range("C2").Value & Range("D2").Value & "\" & Range("E2").Value & Range("D2").Value & ".xls"

I want to open the workbook, but if it is already open, I want to activate it.

I hope that this makes it a little clearer. Thanks for your responses.

Westoner
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

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