Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Check If Any Cell Is Empty

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.
  2. 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.
  3. Write the quickest whole-range check first. Add a procedure like this:
    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
    This gives you a count, not just a yes/no, which is usually more useful. Change the range address to match your data.
  4. 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.
  5. 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:
    Dim lastRow As Long
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = Range("A2:A" & lastRow)
    This avoids counting the thousands of genuinely blank cells below your data as "empty," which would otherwise always return a positive result.
  6. Loop when you need to know which cells are empty. Use IsEmpty to identify individual cells and act on each one:
    Sub 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 Sub
    The False, False arguments strip the dollar signs from the address so it reads as A5 rather than $A$5.
  7. Account for formulas that return an empty string. IsEmpty returns 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. Use Len whenever the cells may hold formulas.
  8. Wrap the check in a reusable function so other procedures can call it:
    Function HasBlanks(rng As Range) As Boolean
        HasBlanks = (Application.WorksheetFunction.CountBlank(rng) > 0)
    End Function
    Then write If HasBlanks(Range("B2:F50")) Then ... anywhere you need it.
  9. 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, so CountBlank will always report blanks. Limit the range to the data area.
  • Using IsEmpty on formula results. A formula returning "" is not an empty cell to IsEmpty. Use Len(c.Value) = 0 instead.
  • 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 ActiveSheet runs against whatever sheet happens to be open. Name the sheet explicitly, for example Worksheets("Data").
  • Forgetting error handling. If a range reference is wrong, the macro stops with a runtime error. A simple On Error GoTo handler that shows a friendly message helps when users run the macro.
  • Saving as .xlsx. This silently discards macros; use .xlsm.

Tips

  • CountBlank is 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 Long variable rather than an Integer; 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_Change event 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.