Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Save As Dialog With Filename

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You get a Save As dialog with a suggested filename by calling Application.GetSaveAsFilename in a VBA macro. The function opens the standard Windows Save As box, lets you pre-fill the file name and file type, and returns the chosen path as a string — or the boolean False if the user clicks Cancel. Your macro then decides what to do with that string, usually by calling Workbook.SaveAs or Workbook.SaveCopyAs. Nothing needs to be installed; the only requirement is that macros are enabled in the workbook where you paste the code.

What You'll Need

  • Excel on Windows or Mac with the Developer tab visible (File > Options > Customize Ribbon > tick Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook saved as .xlsm (macro-enabled), because a plain .xlsx cannot store VBA code.
  • Macro security set to allow your own code to run — the default “Disable all macros with notification” is fine as long as you click Enable Content when prompted.

Step-by-Step

  1. Open the VBA editor. Press Alt + F11 on Windows, or Option + F11 on Mac. In the Project pane on the left, right-click your workbook and choose Insert > Module. A blank code window appears.
  2. Write the procedure. Paste the skeleton below and adjust the strings. The first argument is the suggested filename, the second is the file filter, and the rest are optional.
Sub SaveWithDialog()
    Dim suggestedName As String
    Dim chosenPath As Variant

    suggestedName = "Sales Report " & Format(Date, "yyyy-mm-dd") & ".xlsx"

    chosenPath = Application.GetSaveAsFilename( _
        InitialFileName:=suggestedName, _
        FileFilter:="Excel Workbook (*.xlsx), *.xlsx, Macro-Enabled Workbook (*.xlsm), *.xlsm", _
        Title:="Save the report as...")

    If chosenPath = False Then
        MsgBox "Save cancelled.", vbInformation
        Exit Sub
    End If

    ActiveWorkbook.SaveAs Filename:=CStr(chosenPath)
    MsgBox "Saved to " & chosenPath, vbInformation
End Sub
  1. Understand the return value. GetSaveAsFilename only collects a path — it does not save anything by itself. It returns a Variant holding the full path string, or the Boolean False when the user cancels. Declaring the variable as Variant (not String) is what lets you test for False without a type mismatch error.
  2. Test it. Close the editor, press Alt + F8, select SaveWithDialog and click Run. The dialog should open with your suggested name already highlighted in the File name box.
  3. Choose between SaveAs and SaveCopyAs. SaveAs switches the open workbook to the new file, so later edits go to the new copy. SaveCopyAs writes a duplicate and leaves the original workbook active — better when you want an archive copy without changing where you are working.
  4. Handle the file format. If the user picks a filter entry with a different extension, Excel may prompt about compatibility or refuse the save. To avoid this, either offer a single filter, or pass FileFormat:=xlOpenXMLWorkbook (or xlOpenXMLWorkbookMacroEnabled for .xlsm) to SaveAs so the format matches the extension.
  5. Add a default folder if you like. Put a path in front of the filename, for example "C:\Reports\Sales Report.xlsx", and the dialog opens in that folder. If the folder does not exist, the dialog falls back to the last used location, so create it first with MkDir or check it with Dir.
  6. Guard against overwriting. Excel's own dialog warns before overwriting, but if you build the path yourself with Dir(path) <> "" you can ask for confirmation before calling SaveAs.

Common Mistakes

  • Declaring the result as String. Dim p As String: p = Application.GetSaveAsFilename throws a type mismatch when the user cancels. Always use Variant and compare to False.
  • Forgetting the cancel branch. Without the If chosenPath = False check, the macro carries on and tries to save to a path literally called “False”.
  • Assuming the file is saved. The dialog is a picker, not a saver. If you omit the SaveAs line, the user picks a name and nothing happens.
  • Invalid characters in the suggested name. Characters such as :, /, ?, * and " are rejected by Windows. Strip them, or use Format(Date, "yyyy-mm-dd") rather than a slash-separated date.
  • Saving over the open workbook. Calling SaveAs on the same name with a different format can leave you with a converted file and no easy way back — save a copy when in doubt.
  • Running from a read-only or protected view. Macros in Protected View or a read-only file cannot save; the dialog may open but the save fails.

Tips

  • Build the suggested name from cells or the date so each run proposes something unique: Range("B2").Value & " " & Format(Now, "yyyy-mm-dd hh-nn").
  • Use the Title argument to tell the user what they are saving — “Save the monthly report as…” is clearer than the default caption.
  • Keep the FileFilter short. Two entries are plenty; a long list confuses people and increases the chance of a format mismatch.
  • Assign the macro to a button (Insert > Shapes, right-click, Assign Macro) so colleagues never need the VBA editor.
  • If you want the user to pick a folder only, use Application.FileDialog(msoFileDialogFolderPicker) instead — GetSaveAsFilename always asks for a filename.
  • On Mac, the dialog is the native macOS save panel; the same arguments work, but backslash paths are not valid, so use Application.PathSeparator or a colon-free relative name.

Troubleshooting

The dialog never appears. Check that macros are enabled and that you are running the right procedure. If the workbook opened from an email or download, it may be in Protected View — click Enable Editing first.

“Expected: end of statement” or a compile error on the call. This usually means a missing line-continuation underscore or a stray quote in the filter string. Each continued line must end with a space and an underscore.

Type mismatch at the cancel check. The variable is declared as String. Change it to Variant.

The file saves with the wrong extension. The extension follows the filter the user selected, not the suggested name. Pass an explicit FileFormat to SaveAs, or offer only one filter entry.

“Method 'SaveAs' of object '_Workbook' failed”. Common causes are a read-only file, a folder the user cannot write to, or a filename containing invalid characters. Test the path with Dir and confirm the folder exists.

It works on your machine but not a colleague's. Check their macro security settings and whether the file is stored in a trusted location. Also confirm they are not running the macro from a copy opened read-only from a network share.

Frequently Asked Questions

Does GetSaveAsFilename actually save the file?

No. It only returns the path the user chose. You must call Workbook.SaveAs or SaveCopyAs afterwards.

How do I set the default filename in the dialog?

Use the InitialFileName argument, for example InitialFileName:="Report.xlsx". Include a full path if you want a specific starting folder.

How can I tell if the user pressed Cancel?

The function returns the Boolean False. Store the result in a Variant and test If result = False Then Exit Sub.

Can I limit the dialog to one file type?

Yes. Pass a single pair in FileFilter, such as "Excel Workbook (*.xlsx), *.xlsx". The user can still type another extension manually, so validate if it matters.

Why does the dialog open in the wrong folder?

Excel remembers the last used location. Supply a full path in InitialFileName and make sure that folder exists, otherwise the dialog ignores it.

Can I use this to save as PDF?

You can offer a PDF filter, but SaveAs with a PDF extension needs ExportAsFixedFormat instead. Use the dialog to collect the path, then export to it.