VBA: cancel file selection

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,934
Office Version
  1. 365
  2. 2010
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.
 
At the very top of a standard module (before any code) put
Code:
Global QuitAll As Boolean
In the macro where you are selecting a file use
Code:
Sub macro1()
Dim statements
Application.ScreenUpdating = False
   Set DestWbk = ThisWorkbook
   Fname = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Select a File")
   If Fname = "False" Then
      QuitAll = True
      Exit Sub
   End If
etc
etc
End Sub
and in the main macro use
Code:
Sub main()
   Call macro1
   If QuitAll Then Exit Sub
   Call Macro2
   
End Sub
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
From research I see I can take my first of a few modules and write:

Option Explicit
Global flag Variable As String

Then, use "flag" throughout each of my modules.

I'll have to dig to see how I find out within a sub if the CANCEL key has been clicked when being prompted to select a file from a folder.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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