You can’t just open Excel and hit a button to create a brand-new workbook with a saved filename—no, not without VBA. Here’s how to write a quick, reliable macro that generates a fresh workbook, assigns it a name, and saves it to your preferred location. This isn’t just a tutorial; it’s the working code you can adapt for your workflow, with real-world examples, common pitfalls, and troubleshooting.

What You'll Need

  • A working Excel installation (2010 or later).
  • VBA editor access (press Alt+F11 to open it).
  • Basic familiarity with the Workbooks.Add and Workbooks.SaveAs commands.
  • Knowledge of file paths (e.g., `C:\Users\YourName\Documents\NewFile.xlsm`).

Step-by-Step

  1. Open Excel and press Alt+F11 to open the VBA editor. In the Project Explorer, double-click ThisWorkbook to open its code window.

    If you need to create a new workbook dynamically, use this starter code block as a template:

    Sub CreateAndSaveWorkbook()
        Dim ws As Worksheet
        Dim filePath As String
    
        ' Define where to save the new workbook (adjust path as needed)
        filePath = "C:\YourFolder\NewWorkbook_" & Format(Date, "yyyymmdd") & ".xlsm"
    
        ' Add a new workbook and assign it to a worksheet variable
        Dim newWorkbook As Workbook
        Set newWorkbook = Workbooks.Add()
    
        ' Optional: Rename the workbook to a more descriptive name
        newWorkbook.Name = "SalesData_" & Format(Date, "yyyy-MM-dd")
    
        ' Save the workbook to the specified path
        newWorkbook.SaveAs filePath, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    
        ' Close the workbook without saving (optional)
        Application.DisplayAlerts = False
        newWorkbook.Close SaveChanges:=False
        Application.DisplayAlerts = True
    
        MsgBox "Workbook saved successfully: " & filePath, vbInformation
        Set newWorkbook = Nothing
    End Sub

    Key notes:

    • xlOpenXMLWorkbookMacroEnabled ensures the file extension is `.xlsm` (macros enabled) if you plan to reuse it.
    • If you omit `FileFormat`, Excel defaults to `.xlsx` (read-only).
    • Use `Format(Date, "yyyymmdd")` for a date-based filename; adjust the format string as needed.
  2. Run the macro by pressing F5 in the VBA editor. Excel will create a new, empty workbook and save it to your specified path.

    To customize the location:

    • Replace `"C:\YourFolder\"` with your actual drive/path (e.g., `ThisWorkbook.Path` for the current workbook’s folder).
    • Use ` Environ("USERPROFILE") & "\Documents\"` for a default `Documents` folder location.

    Example for dynamic saving location:

    Sub CreateAndSaveWithCurrentPath()
        Dim newWorkbook As Workbook
        Set newWorkbook = Workbooks.Add()
    
        ' Save to current workbook's folder with timestamp
        newWorkbook.SaveAs ThisWorkbook.Path & "\NewWorkbook_" & Format(Date, "yyyy-MM") & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
    
        ' Rest of the code...
        ' (Close the workbook here)
    End Sub
  3. If you want to reuse the same name (e.g., "ProjectReport") but append a counter:

    Sub CreateReportWithCounter()
        Dim newWorkbook As Workbook
        Dim counter As Integer
    
        ' Start counter at last saved number (or 1 if none exists)
        On Error Resume Next
        counter = Dir(ThisWorkbook.Path & "\ProjectReport_*.xlsm")
        If counter <> "" Then
            counter = Val(Mid(counter, InStr(1, counter, "_") + 1)) + 1
        Else
            counter = 1
        End If
        On Error GoTo 0
    
        Set newWorkbook = Workbooks.Add()
        newWorkbook.Name = "ProjectReport_" & counter & ".xlsm"
        newWorkbook.SaveAs ThisWorkbook.Path & "\ProjectReport_" & counter & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End Sub

Common Mistakes

  • File path errors: Forgetting to include the file extension (e.g., `filePath = "NewWorkbook"` instead of `NewWorkbook.xlsm`). Always end paths with `*.xlsm` or `*.xlsx`.
  • Permission issues: If saving fails, check if Excel has write permissions for the target folder. Run Excel as Administrator if needed (Windows).
  • Macro permissions: Newer Excel versions require a warning before saving macros. Enable it in the macro’s `Application.DisplayAlerts` block as shown in the starter code.
  • Worksheet references missing: If you plan to add data, ensure the macro uses `Set ws = newWorkbook.Sheets.Add()` to create a sheet before populating it.

Tips

  • Use `Application.FileDialog` for interactive file saving:

    Sub SaveWithDialog()
        Dim filePath As Variant
        filePath = Application.GetSaveAsFilename( _
            FileFilter:="Excel Workbooks (*.xlsm), *.xlsm", _
            Title:="Save New Workbook")
    
        If Not IsError(filePath) Then
            Dim newWorkbook As Workbook
            Set newWorkbook = Workbooks.Add()
            newWorkbook.SaveAs filePath, FileFormat:=xlOpenXMLWorkbookMacroEnabled
            newWorkbook.Close SaveChanges:=False
        End If
    End Sub

    This lets users pick a location dynamically.

  • For large datasets, use `Application.EnableEvents = False` in the macro to prevent Excel from freezing while saving.

  • To auto-open a new workbook after saving, add this after `newWorkbook.Close`:

    Application.Run "CreateAndSaveWorkbook

    This creates a second instance of the macro.

Troubleshooting

If your macro fails silently (e.g., no error message), check these:

  1. Error 13 (Type Mismatch): Ensure all variables are properly typed (e.g., `Dim filePath As String`). Use `TypeName(filePath)` to debug.

  2. File not saving: Verify the target folder exists. Add this before saving to test: `If Dir(filePath) = "" Then MkDir Mid(filePath, 1, Len(filePath) - Len(Dir(filePath))) End If`

  3. Macro blocked: In Excel’s Options > Trust Center > Macro Settings, set "Disable all macros with notification" (unless you’re running a trusted source).

  4. Excel hangs: Add `Application.Wait Now + TimeValue("00:00:05")` before `SaveAs` to force a pause (not recommended for production; use `EnableEvents` instead).

For advanced issues, compare the actual code with the starter template and highlight differences. Use `Debug.Print` to log variables:

Debug.Print "Path: " & filePath
Debug.Print "Counter: " & counter

Frequently Asked Questions

1. How do I save the new workbook in a specific folder?

Use a full path like `ThisWorkbook.Path & "\Folders\NewFile.xlsm"` or `Environ("USERPROFILE") & "\Documents\NewFile.xlsm"`. For dynamic paths, use `Application.StartUpPath` or set a constant in a module.

2. Why does my macro always overwrite the same file?

The issue is likely that `newWorkbook.SaveAs` uses the workbook’s current name. Always specify a full path or use `newWorkbook.Name = "CustomName"` before saving. Example: `newWorkbook.SaveAs ThisWorkbook.Path & "\Backup_" & Format(Date, "yyyymmdd") & ".xlsm"`

3. Can this macro work in Excel Online?

No. VBA only runs in installed Excel (Windows/macOS). For Excel Online, use Power Apps or Power Automate instead.

4. How do I add data to the new workbook before saving?

After `Set newWorkbook = Workbooks.Add()`, add a sheet and populate it: `Set ws = newWorkbook.Sheets.Add` `ws.Range("A1").Value = "Hello"` Then save as usual. Use `Application.EnableEvents = False` to prevent Excel from freezing.

5. What if the folder doesn’t exist?

Add this before saving to create the folder if needed: `If Dir(filePath, vbDirectory) = "" Then MkDir Mid(filePath, 1, Len(filePath) - Len(Dir(filePath))) End If`

6. How can I make this macro run at startup?

Add a reference to `ThisWorkbook` in the VBA project’s `Project References`, then run the macro via `ThisWorkbook.Openup` or use the `Startup` event in the Workbook class module.