How stop a code if 'Cancel' is clicked in the file dialog box

JackZill

New Member
Joined
Sep 15, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I'm using the below code to bring up the file dialog box so the user can select a file path to save a file.

What I want to do is stop an error from occuring when the user clicks the 'Cancel' button in the dialog box. Currently when you click 'Cancel', it comes up with 'Run-time error '5': Invalid procedure call or argument' and highlights the last line in the code below. Does anyone know how I can fix this?

Code:
Dim mySaveLocation As String
Dim dlg As FileDialog
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
dlg.AllowMultiSelect = False
dlg.Show
mySaveLocation = dlg.SelectedItems.Item(1)
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi, welcome to the forum!

How about something like this:

VBA Code:
Dim mySaveLocation As String
Dim dlg As FileDialog
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
dlg.AllowMultiSelect = False
dlg.Show
If dlg.SelectedItems.Count > 0 Then
    mySaveLocation = dlg.SelectedItems.Item(1)
End If
 
Upvote 0
Hi, welcome to the forum!

How about something like this:

VBA Code:
Dim mySaveLocation As String
Dim dlg As FileDialog
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
dlg.AllowMultiSelect = False
dlg.Show
If dlg.SelectedItems.Count > 0 Then
    mySaveLocation = dlg.SelectedItems.Item(1)
End If
Hi, thanks for having me! Also thanks for your helpful response.
I added an else exit sub and it works perfectly. Cheers!

Code:
Dim mySaveLocation As String
Dim dlg As FileDialog
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
dlg.AllowMultiSelect = False
dlg.Show
If dlg.SelectedItems.Count > 0 Then
    mySaveLocation = dlg.SelectedItems.Item(1)
    Else
    Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,138
Members
453,021
Latest member
Justyna P

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