Excel VBA Save File Dialog: How to Use GetSaveAsFilename
In this article
In Excel VBA, use Application.GetSaveAsFilename to show a Save As dialog and let the user choose a name and location; it returns the selected path but does not save the workbook. To save after the user confirms, pass that path to Workbook.SaveAs. This distinction lets a macro ask before saving while keeping control of the file format and save operation.
Show a Save As dialog with VBA
The following macro offers an Excel workbook file name, then saves the active workbook only if the user selects a path. If the user clicks Cancel, the macro exits without saving. Before running a macro, save a copy of the workbook: VBA changes cannot be undone with Ctrl+Z.
Sub SaveWorkbookWithDialog()
Dim selectedPath As Variant
Dim fileFormat As XlFileFormat
' Offer a default name and filter the dialog to Excel workbooks.
selectedPath = Application.GetSaveAsFilename( _
InitialFileName:=ThisWorkbook.Name, _
FileFilter:="Excel Workbook (*.xlsx), *.xlsx", _
Title:="Save a Copy As")
' Cancel returns Boolean False; do not treat it as a file path.
If VarType(selectedPath) = vbBoolean Then Exit Sub
fileFormat = xlOpenXMLWorkbook
ThisWorkbook.SaveAs Filename:=CStr(selectedPath), _
FileFormat:=fileFormat
End Sub
Paste the code into a standard module in the VBA editor. In Excel for Microsoft 365 on Windows, press Alt+F11 to open the editor, choose Insert > Module, and paste the macro. Save the macro-enabled source workbook as an .xlsm file so its VBA code is retained. The example saves the workbook containing the macro as an .xlsx file, which does not preserve VBA code in the newly saved copy.
What GetSaveAsFilename does—and does not do
Application.GetSaveAsFilename displays a file dialog and returns the path the user selected. It does not write data to disk, rename the workbook, or save changes. That is why the example makes a separate call to ThisWorkbook.SaveAs.
Excel can return a string containing the chosen full path, or the Boolean value False when the user cancels. The return value is a Variant, so declare the variable as Variant rather than String. Checking whether it is Boolean before using it avoids trying to save to a path that is actually the cancellation result.
The dialog’s file filter controls what file types are displayed; it does not, by itself, set the format used to save the workbook. The FileFormat argument on SaveAs does that. These settings should agree. For example, an .xlsx extension should normally be paired with xlOpenXMLWorkbook; an .xlsm workbook that retains macros requires xlOpenXMLWorkbookMacroEnabled.
Choose the right dialog method
Excel VBA has two commonly used approaches, and they serve different purposes:
Application.GetSaveAsFilename: Returns a proposed or selected path. Use it when the macro should decide what to do next, such as checking the choice, changing the filename, or saving a particular workbook.Workbook.SaveAsby itself: Saves a workbook to a specified path. If you need the user to choose that path, call a dialog method first.Application.FileDialog(msoFileDialogSaveAs): Provides a FileDialog object with properties and methods for configuring and showing a Save As dialog. It can be useful when a macro needs more dialog customization, but it still requires a separate save operation.
Do not confuse the Save As dialog with Application.GetOpenFilename. The latter asks the user to select an existing file to open or process; it is not intended to choose a destination for a new save. Likewise, Workbook.Save saves changes to the workbook’s existing file and does not ask for a new path.
Set a starting name and file type
The InitialFileName argument supplies the starting name or path displayed in the dialog. In the example, ThisWorkbook.Name uses the macro’s workbook name. If you want a new copy with a different name, provide a name such as Monthly Report.xlsx. Avoid assuming a particular folder unless the macro has a valid path for the user’s computer.
FileFilter consists of a description and a wildcard pattern, separated by a comma. For example, Excel Workbook (*.xlsx), *.xlsx displays an Excel workbook filter. A filter helps the user choose a suitable extension, but users may still be able to enter a different name, so set the correct FileFormat when saving.
Common Excel formats include:
xlOpenXMLWorkbookfor.xlsxfiles, which do not store VBA macros.xlOpenXMLWorkbookMacroEnabledfor.xlsmfiles that can store VBA macros.xlExcel12for Excel Binary Workbook, or.xlsb.
When the destination format cannot hold features in the workbook, Excel may warn that some content could be lost. Do not suppress such warnings blindly: check whether the selected format is appropriate before deciding how the macro should handle them.
Save a copy instead of changing the source
ThisWorkbook refers to the workbook containing the VBA code. ActiveWorkbook refers to whichever workbook is currently active, which might be a different file. Use ThisWorkbook when the macro should save its own workbook; use ActiveWorkbook only when the user’s active workbook is deliberately the target.
SaveAs changes the workbook’s current file name and location after a successful save. If the goal is to keep the original open under its existing name and create another file, consider Workbook.SaveCopyAs after obtaining the path. It saves a copy without changing the open workbook’s identity, but it does not provide the same file-format conversion control as SaveAs. Select a filename and extension that match the copy’s actual format.
Saving over an existing file can replace it. Excel may show an overwrite confirmation; whether that appears can depend on the operation and application settings. A macro that disables alerts needs especially careful handling, because suppressing prompts can remove the user’s chance to reconsider an overwrite. Prefer a clear confirmation or a unique proposed filename if replacing a file is not intended.
Common errors and practical safeguards
- Type mismatch after Cancel: The dialog returns Boolean
False, not an empty string. Keep the result in a Variant and test for cancellation before calling a save method. - Wrong extension or format: A filter is not a format conversion. Match the selected extension to the
FileFormatargument and consider whether macros must be preserved. - Unexpected workbook saved: Check whether the code uses
ThisWorkbookorActiveWorkbook; they can identify different files. - Invalid or inaccessible destination: The user may select a location that is unavailable, read-only, or restricted by permissions. A production macro can add error handling around the save and explain that the file was not saved.
- Macro security: Excel may block or warn about macros depending on Trust Center settings and the file’s source. Do not ask users to enable macros for an untrusted workbook.
A dialog is a request for a destination, not proof that the save succeeded. For workflows where saving is critical, handle errors from the save operation and give the user a clear success or failure message. Keep the code’s behavior predictable, especially when working with shared folders or files that may already exist.
Frequently Asked Questions
Does GetSaveAsFilename save the workbook?
No. It returns the path selected in the dialog. Use Workbook.SaveAs to save the workbook to that path, or another appropriate save method if you need different behavior.
How do I cancel the VBA Save As dialog safely?
Store the result in a Variant and check whether it is Boolean before passing it to a save method. Cancel returns False, so the macro should exit or take another deliberate action.
How do I keep VBA macros in the saved file?
Save in a macro-enabled format such as .xlsm, using xlOpenXMLWorkbookMacroEnabled with SaveAs. Saving as .xlsx does not preserve VBA code in that saved file.
Can I use the dialog to choose a folder only?GetSaveAsFilename is designed to return a filename and path. To ask only for a folder, use a folder-selection dialog, such as Application.FileDialog(msoFileDialogFolderPicker), then build a filename from the selected folder.
Does this VBA code work in Excel for the web?
No. Excel for the web can open and edit many macro-enabled workbooks, but it cannot run VBA macros. Use desktop Excel for Windows or Mac to run VBA; dialog behavior and available VBA support can differ between platforms.