VBA Macro that does not run other macros unless file is present

LNG2013

Active Member
Joined
May 23, 2011
Messages
466
I have a macro that runs many more macros (subs) under it.

However, I would like to have the macro only continue to run the subs under it if the folder the macro is in contains a file known as test.csv.

Otherwise, I would want it to end the macro and not execute the rest.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
For instance:

Code:
Const sMyFolder as String = "C:\My files\"
If Len(Dir(sMyFolder & "test.csv")) Then
   Call MyMacro 'this macro will be executed if the file is there
End If
 
Upvote 0
For instance:

Code:
Const sMyFolder as String = "C:\My files\"
If Len(Dir(sMyFolder & "test.csv")) Then
   Call MyMacro 'this macro will be executed if the file is there
End If


Thanks Wigi, but how could I do the same thing using the folder structure the macro is running in?

Like right now it is c:/Macro/files
But it runs on different computers who might have a setup like m:/test/fgdfg/

Is there a term for this ? Folder/Structure independent?
 
Upvote 0
Hi Wigi -

I tried this code as you recommended, substituting out the C:/ with ThisWorkbook.Path.
Unfortunately I get: Compiler Error. Constant expression required.

Code:
Const sMyFolder As String = ThisWorkbook.Path
If Len(Dir(sMyFolder & "Test5.csv")) Then
   Call AMasterCall 'this macro will be executed if the file is there
End If

Hello

If you had followed other topics that are being answered right now (or use the search function), you would have found:

http://www.mrexcel.com/forum/showthread.php?t=565100
 
Upvote 0
In that case, replace

Const sMyFolder As String = ThisWorkbook.Path

with

Dim sMyFolder As String
sMyFolder = ThisWorkbook.Path
 
Upvote 0
Cool Thank you!

I ended up using this code, as it allowed me to display the dialogue for 6 seconds also, then close the macro.

Code:
Sub Edit()

If Dir(ThisWorkbook.Path & "\Test5.csv") <> "" Then
  Call AMasterCall
 Else
CreateObject("WScript.Shell").Popup "Support file Test5.csv is missing from the folder" & ". Macro will now close" & ".", 6, "Missing File"
Application.DisplayAlerts = False
    Application.Quit
End If

End Sub

In that case, replace

Const sMyFolder As String = ThisWorkbook.Path

with

Dim sMyFolder As String
sMyFolder = ThisWorkbook.Path
 
Upvote 0

Forum statistics

Threads
1,224,578
Messages
6,179,652
Members
452,934
Latest member
mm1t1

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