How to Make an Excel Column Required Based on Another Before Saving
Question details
The user needs to prevent a workbook from saving if a specific cell in a dependent column is blank while its corresponding primary column has a value greater than zero.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Enforcing data entry rules across multiple columns so that users cannot save and close an incomplete form if a specific condition (like Zone Number > 0) is met.
- Observed behavior
- Standard cell data validation cannot intercept the file save action. A VBA macro tied to the workbook's save event is required to evaluate the columns and cancel the save process if data is missing.
Ensure that the Developer tab is enabled in your spreadsheet application and that macro security settings are configured to allow VBA scripts to run.
Use the Workbook_BeforeSave VBA Event
Use a VBA script in the ThisWorkbook module to check conditions upon saving and cancel the save if required cells are blank.
By utilizing the Workbook_BeforeSave event, you can intercept the user's attempt to save the file. The macro will scan the designated rows, checking if the primary column meets your criteria (e.g., greater than zero) and if the dependent column is empty. If the condition fails, it displays a warning and cancels the save operation.
Open your workbook, navigate to the Developer tab on the top ribbon, and click 'Visual Basic'. Alternatively, press Alt + F11 on your keyboard to open the VBA editor.
In the Project Explorer pane on the left side of the VBA editor, find your workbook name. Double-click on 'ThisWorkbook' to open its code window.
Paste a Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) procedure. Use a For Each loop to check your target range (e.g., Column C). If the cell value is > 0 and the adjacent cell in Column D is blank (""), trigger a MsgBox warning and set 'Cancel = True' to block the save.
Close the VBA editor. Go to File > Save As, and choose 'Excel Macro-Enabled Workbook (*.xlsm)' from the file format dropdown. Standard .xlsx files cannot store VBA macros.

Automate Data Validation with WPS Spreadsheet
WPS Office provides robust, built-in support for VBA and Macros without requiring an expensive subscription. You can easily implement the Workbook_BeforeSave event to enforce strict column requirements and ensure perfect data integrity.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your existing spreadsheet document.
- 2. Enable the Developer Tools: Navigate to the 'Tools' or 'Developer' tab on the main ribbon and click on 'VBA Editor'.
- 3. Add the Save Event Macro: In the Project window, double-click 'ThisWorkbook', paste your Workbook_BeforeSave validation script, and close the editor.
- 4. Save Your Work: Click 'Save As' and select the 'Macro-Enabled Workbook (*.xlsm)' format to ensure your new validation rules remain active.

Frequently Asked Questions
Can I validate multiple pairs of columns using this VBA method?
Yes. Within the Workbook_BeforeSave event, you can set up multiple ranges or loop through several column pairs. For example, if you have six months of data, you can define an array of the primary columns and their corresponding dependent columns, and loop through each pair before allowing the save.
Why do I get a syntax error when using an underscore (_) in my VBA code?
In VBA, a space followed by an underscore ' _' at the end of a line is a line-continuation character. It tells the compiler that the code continues on the next line. You will receive a syntax error if there is a blank line or an inline comment immediately following the underscore.
Can standard Excel Data Validation prevent the file from saving?
No. Standard Data Validation (found in the Data tab) restricts what users can type into a cell, but it does not intercept workbook events like saving or closing. To actively prevent a file from being saved when cells are blank, you must use a VBA macro.




