Excel VBA If Not Empty: Cell Check Guide
In this article
To test whether a cell is not empty in Excel VBA, use the Len function with the cell's text, the IsEmpty function with the cell's value, or compare against an empty string. The most reliable general-purpose line is If Len(Trim(cell.Value)) > 0 Then, because it treats a cell containing only spaces as empty. IsEmpty only returns True for a cell that has never held any value; a cell that once contained data and was cleared with Delete returns False, which causes the single most common bug in this kind of code. Choose the test based on what you mean by "empty" and whether the cell might hold a number, a date or a formula result.
What You'll Need
- Excel on Windows or Mac with the Developer tab visible. If it is missing, right-click the ribbon and choose Customize the Ribbon, then tick Developer in the right-hand list. Labels can differ slightly by version, device and region.
- A workbook saved as .xlsm (macro-enabled) or .xlsb. An .xlsx file cannot store macros.
- Macro security allowing macros to run. Use the Trust Center settings under File > Options > Trust Center > Trust Center Settings > Macro Settings on Windows; on Mac it is under the Excel menu.
- Basic familiarity with opening the Visual Basic Editor with Alt+F11 (Windows) or Option+F11 (Mac) and inserting a module with Insert > Module.
- A worksheet with some data, including at least one blank cell and one cell that was cleared with the Delete key, so you can see the difference.
Step-by-Step
Open the Visual Basic Editor with Alt+F11 on Windows or Option+F11 on Mac. In the Project pane on the left, find your workbook and use Insert > Module to add a blank module. Code written in a sheet module works too, but a standard module is easier to test and reuse.
Type a complete procedure. VBA needs a starting line and an ending line; without them nothing runs.
Sub CheckCell() Dim target As Range Set target = ActiveSheet.Range("A1") If IsEmpty(target.Value) Then MsgBox "A1 is empty" Else MsgBox "A1 contains: " & target.Value End If End SubPress F5 to run it. Click on a sheet with A1 blank and run again to see the other branch.
Understand what
IsEmptyactually measures. It checks whether a Variant variable holds the special Empty value. A truly untouched cell returns Empty. A cell you cleared by pressing Delete actually holdsvbNullStringfor text, or a zero-length string, soIsEmptyreports False and your "empty" check fails. Test this deliberately: put a value in A1, press Delete, then rerun the macro. It will report that A1 contains something, with the content appearing blank in the message box.Switch to a length-based test when cleared cells or spaces are possible. Replace the condition with
If Len(target.Value) > 0 Then. This returns True whenever the cell holds any character at all, including a literal space. Wrap it inTrimto ignore whitespace:If Len(Trim(target.Value)) > 0 Then. Note thatTrimin VBA strips leading and trailing spaces but not non-breaking spaces (character 160), which often arrive from web copy-paste; if you suspect those,Replace(cell.Value, Chr(160), "")first.Handle numeric and date cells correctly.
Lenworks on numbers because VBA converts the value to text first, but it will throw a type mismatch if the cell holds an error value such as #N/A. Guard against errors by checkingIsError(cell.Value)before testing content. For dates,Lenmeasures the rendered text, so a valid date always tests as non-empty.Decide how to treat formula results. A formula that returns
""shows as blank but is not empty.IsEmptyreports False, andLenreports 0, so the length test treats it as empty. If you need to know whether a formula is physically present, testcell.HasFormulainstead. A cell with=IF(A1="","","Done")sitting over an empty A1 has a formula but no visible content.Loop through a range with the test applied to every cell. This is the practical use case: skip blanks while processing a list.
Sub ProcessColumn() Dim cell As Range Dim count As Long count = 0 For Each cell In Range("A1:A100") If Len(Trim(cell.Value)) > 0 Then count = count + 1 cell.Offset(0, 1).Value = "Filled" End If Next cell MsgBox count & " non-empty cells found" End SubUse the negative form where it reads better:
If Not IsEmpty(cell.Value) ThenorIf Len(cell.Value) > 0 Then. Both are valid VBA; pick one and stay consistent. Avoid writingIf cell.Value <> "" Thenwhen the cell could hold a number, because VBA compares a number to an empty string and can throw a type mismatch in some contexts. Compare as text if you must:CStr(cell.Value) <> "".Add a guard for unqualified Range references. Inside a standard module, bare
Range("A1")refers to the active sheet, which changes when the user clicks around. Qualify it, for exampleThisWorkbook.Worksheets("Data").Range("A1"), or the check will quietly run against the wrong sheet.Save the workbook as .xlsm and test from a fresh window to confirm the macro behaves the same when a different sheet is active. Close the Visual Basic Editor before judging results, since MsgBox dialogs appear behind it on some setups.
Common Mistakes
Using IsEmpty on a cleared cell. As above, this is the biggest trap. Any cell that has ever been edited and then emptied by Delete or Clear Contents is not Empty in the VBA sense. Use the length test instead.
Forgetting that a space is content. A cell that looks blank but contains the spacebar keystroke passes an untrimmed length test. Always trim when validating user-entered data.
Testing the wrong thing. IsEmpty(cell) on the Range object rather than IsEmpty(cell.Value) tests the object variable, which is never Empty once Set. Use the Value property by default.
Ignoring error values. A cell showing #REF! or #N/A breaks text comparisons. Check IsError first.
Assuming hidden sheets are inactive. Code that relies on ActiveSheet misses cells on other sheets; always qualify your references.
Tips
- To count non-empty cells without a loop, use
WorksheetFunction.CountA(range), which counts anything that is not truly blank. - For whole-column checks, limit the range to the used area with
Cells(Rows.Count, 1).End(xlUp).Rowrather than scanning a million rows. - Turn off
ScreenUpdatingwhile looping and restore it afterward so the output appears faster. - Declare variables with explicit types;
Dim cell As Rangelets IntelliSense guide you to the right properties.
Troubleshooting
The macro says the cell is not empty but you see nothing. It almost certainly contains a space, a formula returning "", or a cleared leftover. Test with If Len(cell.Value) > 0 and print the length in a MsgBox to confirm.
The macro highlights nothing and never enters the If block. Check whether the loop is reading the intended sheet. Print target.Address and ActiveSheet.Name to verify.
Compile error: Expected: expression. A missing Then, an unbalanced quote or a missing End If causes this. Step through the code with F8 to find the line.
"Sub or Function not defined". The procedure may sit in the wrong module, or its name collides with an existing one. Rename it and place it in a standard module.
Run-time error 13: Type mismatch. You are comparing a non-text value to a string. Wrap the value in CStr or test the data type with VarType first.
Frequently Asked Questions
Does IsEmpty work on a cell cleared with Delete?
No. It returns False, because the cell holds a zero-length value rather than the Empty variant. Use a length test.
Should I test Length or IsEmpty?
Use Len(Trim(cell.Value)) > 0 for user data, which handles cleared cells and spaces. Reserve IsEmpty for genuinely untouched variables.
Why does a formula returning an empty string count as not empty with IsEmpty?
Because the cell contains a formula and a value, even if that value is "". Len treats it as empty; HasFormula tells you it exists.
How do I check a whole row for blanks?
Use WorksheetFunction.CountA on the row range and compare to the expected column count, or loop each cell with the length test.
Can I check if an array or variable is empty here?
For variables, IsEmpty(var) is correct and reliable since variables have no cell quirk. For arrays, test their bounds with LBound and UBound inside an error handler.