VBA & Macro Problems

How to Make Required Excel Cells Mandatory Before Saving

Bushra ParveenBushra Parveen Nov 2, 2026 869 views

Question details

The user wants to ensure that specific required cells in partially completed rows are filled out before the workbook can be saved.

How to Make Required Excel Cells Mandatory Before Saving
Product
Excel
Device & OS
not provided
Scenario
When users fill out a shared tracker or form, they often leave critical fields blank. The goal is to enforce data entry for active rows without blocking the save function for completely blank new rows.
Observed behavior
By default, Excel allows saving regardless of whether cells are blank. The user needs a custom validation method to intercept the save action, warn the user, and cancel the save if required data is missing.
Before you start

Ensure you are prepared to save your workbook in a Macro-Enabled format (.xlsm), as standard Excel files (.xlsx) will automatically delete your VBA code upon closing.

Solution 1Recommended

Use the Workbook_BeforeSave VBA Event

Add a customized macro to the ThisWorkbook module that evaluates rows and blocks the save action if required fields are blank.

Since standard Data Validation cannot prevent a user from saving a file with empty cells, VBA is required to intercept the save action. By using the Workbook_BeforeSave event, you can command Excel to evaluate specific cell ranges right before the save executes.

1
Open the VBA Editor

Open your workbook and press the 'ALT + F11' keys simultaneously to open the Visual Basic for Applications (VBA) editor.

2
Access the ThisWorkbook Module

In the Project Explorer pane on the left side of the VBA editor, double-click on 'ThisWorkbook' to open its code window.

3
Set Up the BeforeSave Event

At the top of the code window, change the left dropdown menu from '(General)' to 'Workbook'. The right dropdown will automatically change to 'Open'. Click the right dropdown and select 'BeforeSave'.

4
Input the Validation Logic

Inside the Private Sub Workbook_BeforeSave routine, write code that uses WorksheetFunction.CountBlank to check your required ranges. If CountBlank returns a value greater than 0 for an active row, display a MsgBox warning and add the line 'Cancel = True' to abort the save.

5
Save as a Macro-Enabled Workbook

Close the VBA editor. Go to File > Save As, change the 'Save as type' dropdown to 'Excel Macro-Enabled Workbook (*.xlsm)', and click Save.

Use the Workbook_BeforeSave VBA Event
Macro Security: For this solution to work, anyone opening the file must click 'Enable Content' or 'Enable Macros'. If macros are disabled, the BeforeSave event will not trigger.
Powerful Spreadsheet Automation

Automate Data Entry Forms with WPS Spreadsheet

WPS Spreadsheet features an integrated, highly compatible Visual Basic Editor, allowing you to easily add custom BeforeSave macros to make required cells mandatory. It effortlessly handles Microsoft Excel macro formats.

  1. 1. Open your Document in WPS: Launch WPS Office and open your spreadsheet file.
  2. 2. Access the Developer Tools: Click on the 'Tools' tab on the ribbon, then click on 'Developer' to access the macro environment.
  3. 3. Open the Visual Basic Editor: Click 'Visual Basic Editor' (or press ALT + F11). Double-click 'ThisWorkbook' in the project window.
  4. 4. Add the Code and Save: Paste your BeforeSave macro code. Go to Menu > Save As and select the 'Microsoft Excel Macro-Enabled Workbook (*.xlsm)' format to ensure your script is saved.
Fully compatible with Microsoft Excel .xlsx and .xlsm formatsBuilt-in VBA editor for running and writing complex macrosFree to use with a lightweight installation and fast performanceFamiliar user interface requiring no learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Can I use Data Validation instead of VBA to make cells mandatory?

Standard Data Validation restricts what users can type into a cell, but it cannot prevent a user from leaving a cell completely blank and saving the file. To completely block a file from saving due to empty required cells, VBA is necessary.

Why did my macro disappear after I saved and closed the file?

If you save your file as a standard Excel Workbook (.xlsx), all macros are automatically stripped out. You must choose 'Save As' and select 'Excel Macro-Enabled Workbook (*.xlsm)' to retain your VBA code.

How do I ensure entirely blank rows aren't flagged as incomplete?

In your VBA logic, you should first check if all cells in a specific row are empty. If the row is completely blank, instruct the macro to skip it and move to the next row, only triggering the 'Cancel = True' command if a row is partially filled.