logo
search
VBA & Macro Problems

Fix SaveAsUI Undefined Error in Excel VBA Event

Steve KSteve K Sep 27, 2026 869 views

Question details

The user needs to resolve an issue where the SaveAsUI variable is reported as undefined when using the Workbook_BeforeSave event in VBA.

How to Fix "SaveAsUI Undefined" Error in Excel VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Writing or executing a VBA macro that relies on the Workbook_BeforeSave event with 'Option Explicit' enabled.
Observed behavior
VBA throws a compile error stating that SaveAsUI is undefined, typically caused by syntax errors or incorrect placement of the event procedure header.
Before you start

Make sure your workbook is saved in a macro-enabled format (.xlsm) and that you have access to the Developer tab to open the VBA Editor.

Solution 1Recommended

Correct the Workbook_BeforeSave Event Declaration

Fix the undefined variable error by placing the properly formatted event procedure directly into the ThisWorkbook module.

When 'Option Explicit' is enabled, VBA requires all variables to be declared. If the BeforeSave event header is typed incorrectly or placed in a standard module, Excel fails to recognize the built-in parameters like SaveAsUI and Cancel, resulting in compile errors.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor in Excel.

2
Access the ThisWorkbook Module

In the Project Explorer pane on the left side, locate your active workbook. Double-click on 'ThisWorkbook' to open its specific code window. Do not use a standard module (like Module1) for workbook events.

3
Insert the Correct Procedure Header

Type or paste the following exact declaration into the code window: Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean).

4
Remove Syntax Errors

Ensure there is a space between 'ByVal' and 'SaveAsUI' (no underscores) and verify that 'Cancel As Boolean' is included before the closing parenthesis. Save the VBA project.

Correct the Workbook_BeforeSave Event Declaration
Automatic Execution: Excel triggers the Workbook_BeforeSave event automatically whenever the workbook is being saved. You should never attempt to call this procedure manually from other macros.
Free Microsoft Office alternative

Try WPS Office for a Seamless Spreadsheet Experience

If you are looking for a reliable, lightweight, and highly compatible alternative to Microsoft Excel, WPS Office is an excellent choice. It handles complex spreadsheets and supports advanced functionalities while offering a familiar interface that requires zero learning curve.

  1. 1. Download and Install WPS Office: Visit the official WPS website to download the free suite and follow the quick installation wizard.
  2. 2. Open Your Spreadsheet: Launch WPS Spreadsheets and open your existing .xlsx or .xlsm files directly.
  3. 3. Work Without Interruptions: Enjoy full compatibility with your existing data, formulas, and formatting in a streamlined environment.
Highly compatible with Microsoft Excel formats (.xlsx, .xlsm, .csv).Familiar user interface allows for seamless migration without relearning.Lightweight and fast, ensuring smooth performance even on older devices.Free to download and use for your daily spreadsheet and office tasks.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Option Explicit cause an undefined error for SaveAsUI?

Option Explicit forces all variables to be explicitly declared before they run. If the event procedure header is malformed or placed in the wrong module, VBA doesn't recognize SaveAsUI as a built-in parameter, treating it as an undeclared variable and throwing an error.

Can I place the BeforeSave event in a standard VBA module?

No. Workbook-level events like Workbook_BeforeSave must be placed inside the 'ThisWorkbook' module. If placed in a standard module, Excel will not recognize the code as an event handler and it will not run automatically upon saving.

How do I fix the 'expected list separator' error in the VBA procedure header?

This error occurs when the syntax is incorrect. Check for missing commas between parameters, undeclared variables (like missing 'Cancel As Boolean'), or accidental underscores between keywords such as 'ByVal_SaveAsUI'. Using the standard header resolves this.