Calling Multiple files and exiting if file doesn't exist

Ananthak275

Board Regular
Joined
Aug 22, 2020
Messages
128
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
My VBA script calls multiple macros, one of them is to open a file and if one of them doesn't exists then exits the sub. The following macros are dependent on the first macro calling the file location, and even if that sub exits, the following macros still run.

Sub Call_Process ()
Call File_Location
Call Open_File
Call Exit_WB
End Sub

Sub File_Location ()
location_db = "C:\Users\Documents\New folder\*.xl??"
End Sub ()

Sub Open_File ()
dim wb as Workbook
dim ws as Worksheet

if dir(location_db) = "" <- as you can see if the file doesnt exists, it exits out of Open_File sub but not the Exit_WB when im running the Call_Process
end sub
end if

wb_filename = wb.name
sheet_name = ws.name
End Sub


Sub Exit_WB
Workbooks(location_db).close savechanges=TRUE
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
As a Sub procedure can have only a unique End Sub codeline so you must remove the one within the If … End If !​
In fact you need an Exit Sub codeline …​
 
Upvote 0
As a Sub procedure can have only a unique End Sub codeline so you must remove the one within the If … End If !​
Yeah it exits out of that Macro, but when im using the Sub Call_Process (), it will move onto the next one. How do i stop it from going to the next macro if the file doesnt exist?
 
Upvote 0
Transform each Sub to Function returning for example True of False whatever in order to test if the next one should be launched​
or as it is just use a public variable in order to check it before launching the next Sub …​
Or the obviously obvious : use a single procedure rather than multiple Sub !​
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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