Excel VBA Save As Dialog: How to Create and Use It
In this article
To trigger a Save As dialog in Excel VBA, use the Application.GetSaveAsFilename method. This command opens the standard Windows file explorer window, allowing the user to select a folder, type a filename, and choose a file extension, which the VBA script then captures as a string variable to be used in a Workbook.SaveAs command.
What You'll Need
- Microsoft Excel installed on a Windows PC.
- The Developer tab enabled in the Excel Ribbon.
- A basic understanding of how to open the Visual Basic Editor (VBE).
- A workbook that you intend to save via the script.
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard. This opens the VBE window where all your macros are written. - Insert a New Module: In the top menu, go to
Insert > Module. This creates a blank space where you can write your code without interfering with specific sheet or workbook events. - Declare Your Variables: At the top of the module, create a variable to hold the file path. Use a String variable because the dialog box returns the chosen path as text.
Dim fileSaveName As Variant
UsingVariantis critical here because if the user clicks "Cancel", the method returnsFalse(a boolean), not a string. - Configure the GetSaveAsFilename Method: Write the line of code that triggers the window. You can specify a default filename and a file filter to restrict the user to specific formats (like .xlsx or .pdf).
fileSaveName = Application.GetSaveAsFilename(InitialFileName:="MyReport.xlsx", FileFilter:="Excel Workbook (*.xlsx), *.xlsx")
TheInitialFileNameargument suggests a name to the user, and theFileFilterensures they save it in the correct format. - Handle the "Cancel" Action: You must check if the user actually selected a file or simply closed the window. If you don't do this, the code will try to save the file to a path called "False", which will cause a runtime error.
If fileSaveName = False Then Exit Sub
This line tells Excel to stop the macro immediately if the user cancels the operation. - Execute the SaveAs Command: Now that you have the path stored in
fileSaveName, tell Excel to actually save the workbook to that location.ActiveWorkbook.SaveAs Filename:=fileSaveName
This takes the string captured from the dialog and applies it to the current active workbook. - Add a Confirmation Message: To ensure the user knows the process worked, add a simple message box at the end.
MsgBox "File saved successfully!", vbInformation - Test the Macro: Close the VBE window, return to Excel, press
Alt + F8, select your macro, and click "Run".
Common Mistakes
One of the most frequent errors is declaring the fileSaveName variable as a String instead of a Variant. When a user clicks "Cancel" in the Save As dialog, Excel returns a Boolean value of False. If your variable is strictly a String, VBA will throw a "Type Mismatch" error before the code even reaches your check for the cancel button. Always use Variant for dialog return values.
Another common mistake is forgetting to specify the FileFilter correctly. The syntax for the filter is very specific: it requires a descriptive name followed by a comma and then the actual extension (e.g., "Text Files (*.txt), *.txt"). If you omit the comma or the asterisk, the dialog may not filter files correctly, or it may not show the expected file extensions in the dropdown menu.
Users also frequently forget to handle the ActiveWorkbook vs ThisWorkbook distinction. ThisWorkbook refers to the workbook where the code is actually written, while ActiveWorkbook refers to whatever file is currently on top of the screen. If your macro is in a "Personal Macro Workbook" but you want to save the data file you are looking at, you must use ActiveWorkbook.
Tips
To make your tool more professional, you can dynamically generate the InitialFileName. Instead of hardcoding "MyReport.xlsx", you can use a cell value. For example, InitialFileName:=Range("A1").Value & ".xlsx". This allows the user to have a suggested filename based on the data inside the sheet, such as a client name or a date.
If you need to save the file in a specific format other than the default (like a CSV or a Binary workbook), you must include the FileFormat argument in the SaveAs method. For example, to save as a CSV, use ActiveWorkbook.SaveAs Filename:=fileSaveName, FileFormat:=xlCSV. Without this, Excel may save the file with a .csv extension but keep the internal XML structure of a standard workbook, making the file unreadable by other software.
Consider adding Application.ScreenUpdating = False at the start of your macro and setting it back to True at the end. While the dialog box itself is a separate window, disabling screen updating prevents the background Excel sheet from flickering or refreshing while the save process is occurring, providing a smoother user experience.
Troubleshooting
The macro crashes when I click Cancel: This is almost always due to the variable type. Ensure Dim fileSaveName As Variant is used. If you used As String, change it to Variant and ensure the If fileSaveName = False Then Exit Sub check is present immediately after the dialog call.
The file saves with a weird extension or won't open: This happens when the FileFilter in the dialog doesn't match the FileFormat in the SaveAs command. Ensure that if you are asking the user to save as a PDF or CSV, you are using the corresponding xl constant (like xlCSV or xlTypePDF) in the final save line.
The dialog box opens in a random folder: GetSaveAsFilename usually opens the last folder accessed by the user in that session. If you want it to open in a specific folder, you must use the ChDir command to change the current directory before calling the dialog. Note that ChDir only works for folders on the current drive; if you need to change drives, use ChDrive "D" first.
Permission Denied Error: This occurs if the user selects a folder where they do not have write permissions (like the root of the C: drive) or if the file they are trying to overwrite is currently open in another program. You can wrap your save command in an On Error Resume Next block and check Err.Number to provide a custom warning to the user that the location is invalid.
Frequently Asked Questions
Can I use this to save as a PDF?
The GetSaveAsFilename method only captures the name and path. To actually save as a PDF, you must use the ActiveSheet.ExportAsFixedFormat method using the path captured from the dialog.
Does this work on Mac?
No, Application.GetSaveAsFilename is a Windows-specific method. Mac users will need to use AppleScript via the MacScript function or different VBA commands to handle file paths, as the file system architecture differs significantly.
Can I force the user to save in a specific folder?
You cannot "lock" the dialog to one folder, but you can use ChDir to make that folder the default starting point. The user can still navigate away from it unless you write a validation loop that checks the path and re-opens the dialog if the path is incorrect.
How do I save multiple sheets into different files using one dialog?
You would need to wrap the logic in a For Each loop. However, calling the dialog inside a loop will prompt the user for a name every single time. It is better to ask for a folder path once using FileDialog(msoFileDialogFolderPicker) and then save the files automatically using the folder path and a naming convention.
What happens if the file already exists?
Excel's built-in SaveAs method will automatically trigger a standard Windows prompt asking the user if they want to overwrite the existing file. You do not need to write extra code to handle this unless you want to suppress the warning.