VBA: cancel file selection

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,940
Office Version
  1. 365
I have a macro that prompts a user to select a file that is subsequently saved as a sheet and the data used in calculations.
If the user cancels at the prompt to select a file, how do I avoid an error and just exit the macro?

I suspect it's something to do with OnError, but don't know the placement or syntax to use.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Can you post the code you're using?
 
Upvote 0
Part of the code:

Code:
Dim statements
Application.ScreenUpdating = False
   Set DestWbk = ThisWorkbook
   Fname = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Select a File")
   If Fname = "False" Then Exit Sub
etc
etc

When this prompt appears, a user might decide to skip selecting a file, so they click "Cancel" (maybe they forgot which one to use or something).
 
Last edited:
Upvote 0
What have you declared Fname as?
 
Upvote 0
In that case your code works for me. In what way doesn't it work for you?
 
Upvote 0
That code (which is a macro called "GetFile") where the user is given the prompt is called from another macro called "RunMacro".

If the user selects a file, the next macro that's run in "RunMacro" is one that does a variety of calculations on the input file and creates another sheet in the workbook.
That macro is called "FixData"...and it errors if the user clicked "CANCEL" on the file selection prompt.

So, I'm pretty sure I WHERE the problem is but not how to fix it.

The FixData macro is still called even when the user clicks CANCEL on the file selection prompt. I don't want that call to be made but want a complete exit from both the FixData macro AND the macro calling it ("RunMacro").
I guess my overall structure isn't ideal!
 
Upvote 0
I've seen a similar question asked elsewhere but there are never any applicable solutions. It seems (at least to me) that there should be a "simple" grand exit upon a condition.
If MACRO_A calls 4 other macros, MAC1, MAC2, MAC3, then MAC4 -- in sequence and, along the way, the user does something (in my case clicks "cancel" on a "select a file" prompt) that I thought there must be a way to totally bail out of not only the currently running macro (e.g., suppose that's MAC3 in the sequence), but out of the calling master macro as well so as NOT to proceed to MAC4, etc. Maybe not.
 
Last edited:
Upvote 0
Two options that I can think of.
1) Use End rather than Exit Sub, but that will also destroy any global/public variables. (Not recommended)
2) Use a global variable & if the user hits Cancel set the variable to TRUE, then check that variable in the master macro after each sub event has been called.
 
Upvote 0
Where do I declare a global variable? Is it before the initial macro? Then, what do I use to detect the CANCEL key has been clicked? Thanks for your helpful suggest. If I can learn how to do this, I suspect that's the key to the issue!!

I have a button on the master sheet that calls the RunMacros routine...then, that routine has the other 3 or 4 sequential macro calls.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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