VBA & Macro Problems

Stop an Excel VBA Macro When Required Cells Are Blank

Anum MushtaqAnum Mushtaq Nov 20, 2026 869 views

Question details

Add validation that stops an Excel VBA macro when required cells are blank and displays a specific warning.

Stop an Excel VBA Macro When Required Cells Are Blank
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the macro

In Excel, open Developer > Visual Basic, then locate the procedure that creates the workbook, saves the file, or sends the email.

2
Insert the validation

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

3
Test the checks

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.

Add required-cell checks at the start of the Excel VBA procedure
Check formula errors: The example treats empty strings and spaces as blank, but it does not handle Excel error values such as #N/A. Resolve those errors or add separate error handling before converting cell values to text.
Free Microsoft Office alternative

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.

Review and edit spreadsheet data in a familiar interface.Open and work with Microsoft Excel XLS and XLSX files.Use a free, lightweight office suite for everyday document tasks.Review required spreadsheet fields before sharing a workbook.
QA img-9

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.