How to Stop a VBA Macro When a Cell Is Empty in Excel and WPS
Question details
The user needs to halt the execution of an Excel VBA macro if a specific cell, such as a payment method field in an invoice, is left blank.

- Product
- Spreadsheet
- Device & OS
- not provided
- Scenario
- Running a VBA macro for an invoice where a payment method must be filled out in a specific merged cell range before the automation code proceeds.
- Observed behavior
- The macro needs to actively check if the target cell is empty, display a warning message box, and exit the procedure to prevent incomplete data processing.
Before modifying your macro, ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and verify the exact worksheet name and cell reference you need to validate.
Use an If Statement and Exit Sub to Halt the Macro
Insert a conditional check at the very beginning of your macro to evaluate the cell's value and exit the subroutine if it is empty.
By placing a validation check at the start of your code, you prevent the macro from executing any further actions if required data is missing. The 'Exit Sub' command immediately terminates the macro, while 'MsgBox' alerts the user about the missing information.
Press Alt + F11 on your keyboard to open the Visual Basic Editor, and double-click the module containing your invoice macro from the Project Explorer pane.
Place your cursor immediately below the 'Sub YourMacroName()' line. Type the validation code: If Sheet2.Range("C15").Value = "" Then
On the next lines, add the following code to display a warning and stop execution: MsgBox "Cannot run the macro because C15 is empty!", vbInformation Exit Sub End If
Click the Save icon in the VBA Editor, return to your spreadsheet, and run the macro with cell C15 empty to verify that the message box appears and the macro stops.

Validate Data and Run Macros Flawlessly with WPS Office
WPS Spreadsheet fully supports VBA macros, allowing you to automate tasks and enforce strict data entry rules. You can seamlessly write, edit, and debug your existing macros while enjoying a modern, lightweight interface.
- 1. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the VBA macros.
- 2. Access the Developer Tab: Navigate to the Developer tab on the top ribbon. If it is not visible, enable it from the WPS settings menu.
- 3. Open the VBA Editor: Click on 'Visual Basic Editor' to view and edit your macro code, where you can easily insert cell validation logic.
- 4. Run and Automate: Save your code and run the macro directly within WPS Spreadsheet to ensure your data validation works perfectly.

Frequently Asked Questions
Why does my macro fail when referencing a merged cell?
When working with merged cells in VBA, Microsoft Excel and WPS Spreadsheet treat the top-left cell of the merged area as the address containing the value. If C15 and D15 are merged, attempting to read the value of D15 will return empty. Always use Range("C15") to check the value.
How do I check if multiple cells are empty before running a macro?
You can use the 'Or' operator to expand your If statement. For example: If Range("C15").Value = "" Or Range("F15").Value = "" Then MsgBox "Please fill all fields!" Exit Sub End If. This will stop the macro if either cell is blank.
Does WPS Office support Excel VBA macros?
Yes, WPS Office provides robust support for VBA macros. You can open, edit, and execute standard Microsoft Excel .xlsm files directly in WPS Spreadsheet without needing to rewrite your code.




