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

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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