Excel VBA: Check If Any Cell Is Empty
In this article
To check whether any cell in a range is empty in Excel VBA, loop through the cells and test each one with IsEmpty, or use WorksheetFunction.CountBlank for a one-line check. The second method is the fastest when you only need a yes/no answer: if Application.WorksheetFunction.CountBlank(Range("A1:A20")) > 0 is true, at least one cell in that range is blank. Loop with IsEmpty when you also need to know which cells are empty, highlight them, or handle each one differently.
Both approaches are built into Excel itself, so nothing extra needs installing. The code below uses the standard Visual Basic Editor that ships with every desktop version of Excel. Note that VBA does not run in Excel for the web, on Excel for iPad or iPhone, or in Excel for Android, so the macros described here are for Excel on Windows and Excel on Mac.
What You'll Need
- Excel for Windows or Excel for Mac (desktop). VBA is not available in the browser or mobile versions.
- A workbook saved in a macro-enabled format (.xlsm) if you want to keep your code after closing the file.
- The Developer tab visible, or you can open the editor with the Alt+F11 shortcut on Windows (Fn+Alt+F11 on some Mac keyboards).
- A clear idea of the range you want to test, for example A2:A100 or B2:F50.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 on Windows. On a Mac you can use the Developer tab and choose Visual Basic, and the menu labels can differ slightly between versions, so look for the editor command in the Developer tab if the shortcut does not respond.
- Insert a standard module. In the editor, choose Insert > Module. A blank code window opens. Keep your code in a module rather than in a sheet or workbook object unless you specifically want it to fire on an event.
- Write the quickest whole-range check first. Add a procedure like this:
This gives you a count, not just a yes/no, which is usually more useful. Change the range address to match your data.Sub CheckBlanks() Dim rng As Range Dim blanks As Long Set rng = ActiveSheet.Range("A2:A100") blanks = Application.WorksheetFunction.CountBlank(rng) If blanks > 0 Then MsgBox blanks & " empty cell(s) found in " & rng.Address Else MsgBox "No empty cells in " & rng.Address End If End Sub - Run it and read the result. Press F5 with the cursor inside the procedure, or click the green Run button on the toolbar. A message box reports how many cells are empty. If the number looks wrong, check that you are on the sheet you expect, because the code uses
ActiveSheet. - Test only the used part of a column. If you do not know where the data ends, build the range from the last populated row instead of hard-coding a fixed address:
This avoids counting the thousands of genuinely blank cells below your data as "empty," which would otherwise always return a positive result.Dim lastRow As Long lastRow = Cells(Rows.Count, "A").End(xlUp).Row Set rng = Range("A2:A" & lastRow) - Loop when you need to know which cells are empty. Use
IsEmptyto identify individual cells and act on each one:
TheSub ListEmptyCells() Dim c As Range Dim msg As String For Each c In Range("A2:A100") If IsEmpty(c.Value) Then msg = msg & c.Address(False, False) & vbCrLf End If Next c If Len(msg) > 0 Then MsgBox "Empty cells: " & vbCrLf & msg Else MsgBox "All cells are filled." End If End SubFalse, Falsearguments strip the dollar signs from the address so it reads as A5 rather than $A$5. - Account for formulas that return an empty string.
IsEmptyreturns False for a cell containing a formula that produces "" (an empty string). If formula-generated blanks should count as empty, test the value instead:If Len(c.Value) = 0 Then. UseLenwhenever the cells may hold formulas. - Wrap the check in a reusable function so other procedures can call it:
Then writeFunction HasBlanks(rng As Range) As Boolean HasBlanks = (Application.WorksheetFunction.CountBlank(rng) > 0) End FunctionIf HasBlanks(Range("B2:F50")) Then ...anywhere you need it. - Save in a macro-enabled format. Use File > Save As and pick Excel Macro-Enabled Workbook (.xlsm). Saving as a plain .xlsx removes the code the next time the file is opened.
Common Mistakes
- Testing the whole column.
Range("A:A")contains over a million cells, soCountBlankwill always report blanks. Limit the range to the data area. - Using
IsEmptyon formula results. A formula returning "" is not an empty cell toIsEmpty. UseLen(c.Value) = 0instead. - Confusing spaces with empty. A cell holding a single space is not blank. Trim before testing if your data may contain stray spaces:
Len(Trim(c.Value)) = 0. - Assuming the active sheet is the right one. Code tied to
ActiveSheetruns against whatever sheet happens to be open. Name the sheet explicitly, for exampleWorksheets("Data"). - Forgetting error handling. If a range reference is wrong, the macro stops with a runtime error. A simple
On Error GoTohandler that shows a friendly message helps when users run the macro. - Saving as .xlsx. This silently discards macros; use .xlsm.
Tips
CountBlankis far quicker than a loop over thousands of rows because the work happens inside Excel rather than row by row.- Assign the result to a
Longvariable rather than anInteger; a range can hold more blanks than an Integer can store on large sheets. - Use
rng.SpecialCells(xlCellTypeBlanks)to select or act on all blank cells at once, but note it raises an error if there are no blanks, so guard it with the count check first. - If you want the check to run automatically, place a call in the
Worksheet_Changeevent of the sheet, but keep those events short and disable them while your code writes to cells to avoid loops. - Test on a copy of the workbook first. Macros that change cell values cannot be undone with Ctrl+Z.
Troubleshooting
The macro does nothing when I press F5. Make sure the cursor is inside the procedure and that the procedure is in a standard module, not in a sheet's code window. Check there are no compile errors using Debug > Compile VBAProject.
"Sub or Function not defined." A procedure name is misspelled or the module does not contain it. Names must match exactly.
CountBlank returns more blanks than expected. Your range extends past the data, or cells contain spaces or formulas returning "". Narrow the range and switch to the Len test where needed.
"Method 'Range' of object 'Worksheet' failed." The range address is malformed, often a missing column letter or mismatched quotes. Print the address in a message box to confirm it.
No Developer tab and Alt+F11 does nothing. Enable the Developer tab through File > Options > Customize Ribbon and tick Developer. On managed workstations, macro settings may be locked by policy; check with your administrator before changing Trust Center settings.
If that did not work, confirm nothing else is blocking macros: open File > Options > Trust Center > Trust Center Settings and review the macro settings. Only enable macros in workbooks you created or trust, and never lower security settings for files from unknown sources.
Frequently Asked Questions
Does IsEmpty work on a range?
No. IsEmpty tests a single value, so it must be applied inside a loop, cell by cell. For a whole range, use CountBlank.
What is the difference between CountBlank and CountA?
CountBlank counts empty cells in a range; CountA counts cells that contain anything. Comparing them across the same range tells you whether any cell is empty.
Can I check for empty cells without VBA?
Yes. Conditional formatting, COUNTBLANK in a worksheet formula, or Go To Special > Blanks all highlight blanks without writing code.
Why does a cell with a formula count as filled?
Because the cell contains a formula, even if it returns "". Test the result with Len instead of IsEmpty.
Will this work in Excel for the web?
No. VBA macros do not run in Excel for the web or the mobile apps; use worksheet formulas there instead.
How do I check several ranges at once?
Call HasBlanks once per range and combine the results with Or, or use the Union method to treat several ranges as one.