Stop an Excel VBA Macro When Required Cells Are Blank
Question details
Add validation that stops an Excel VBA macro when required cells are blank and displays a specific warning.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A macro must check required worksheet cells before creating a workbook, saving a file, or sending an email.
- Observed behavior
- Each blank required cell should trigger its own warning, and the macro should stop before performing subsequent actions.
Identify the worksheet and required cell addresses, and make sure you can open the macro in the Visual Basic editor. Replace the example sheet name and warning messages with those used in your workbook.
Add required-cell checks at the start of the Excel VBA procedure
Use this approach to show a custom warning and exit the macro as soon as a required cell is blank or contains only spaces.
In Excel, open Developer > Visual Basic, then locate the procedure that creates the workbook, saves the file, or sends the email.
Inside the procedure, after any variable declarations and before those actions, add the following code. Change Sheet1 to the name of the worksheet containing the required cells: Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") If Trim(CStr(ws.Range("H7").Value)) = "" Then MsgBox "Team number must be selected", vbExclamation Application.Goto ws.Range("H7"), True Exit Sub End If If Trim(CStr(ws.Range("H8").Value)) = "" Then MsgBox "Complete the required field in H8", vbExclamation Application.Goto ws.Range("H8"), True Exit Sub End If If Trim(CStr(ws.Range("H9").Value)) = "" Then MsgBox "Complete the required field in H9", vbExclamation Application.Goto ws.Range("H9"), True Exit Sub End If
Leave H7 blank and run the macro from Developer > Macros. Confirm that the warning appears and no later action occurs. Then fill H7 and repeat with H8 and H9, including a cell containing only spaces.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Microsoft Office formats, making it easy to bring existing files into your workflow. It does not fix an Excel VBA macro, and macro support and behavior may differ.

Frequently Asked Questions
Where should I put the validation code?
Place it inside the relevant Sub procedure, after variable declarations and before the code that copies a workbook, saves a file, or creates an email.
Will the check catch a cell that contains only spaces?
Yes. Trim removes ordinary leading and trailing spaces, so Trim(CStr(ws.Range("H7").Value)) = "" also identifies a cell containing only spaces.
What happens if more than one required cell is blank?
The checks run in order. The macro displays the warning for the first blank cell, navigates to it, and stops with Exit Sub. Run the macro again after completing that field to check the next one.
Does the check work when a formula returns an empty string?
Yes. If the formula's result is an empty string, the Value check treats the cell as blank. Formula error values need separate handling.
How do I validate cells on a different worksheet?
Change the worksheet name in ThisWorkbook.Worksheets("Sheet1") to the name of the sheet containing the required cells. ThisWorkbook refers to the workbook containing the VBA code.




