Excel VBA: How to Check If a Cell Is Empty
In this article
To test whether a cell is empty in Excel VBA, compare it to the empty string with If Range("A1").Value = "" Then, or use If IsEmpty(Range("A1").Value) Then. The first check catches anything that looks blank, including formulas that return "". The second checks only for a truly empty Variant. Pick one deliberately, because they behave differently, and most real bugs in this area come from mixing them up.
What You'll Need
- Excel with macros enabled (any desktop version on Windows or Mac will do; the VBA editor is not available in Excel for the web).
- A workbook saved as .xlsm if you want the macro to stay inside it. Standard .xlsx files cannot store macros.
- The Developer tab visible, or the shortcut Alt+F11 (Windows) to open the VBA editor. On a Mac, use Tools > Macro > Visual Basic Editor. Labels can differ slightly by version, device and region.
- A module to write code in: in the editor, Insert > Module.
Step-by-Step
- Open the editor and add a module. Press Alt+F11 in Excel, then choose Insert > Module. A blank code window appears. Type your test code there rather than in a worksheet or ThisWorkbook module, so it is easy to find later.
- Write the basic empty-string test. Enter this and press F5 to run it:
Sub CheckEmpty() If Range("A1").Value = "" Then MsgBox "A1 is empty" Else MsgBox "A1 contains: " & Range("A1").Value End If End SubThis compares the cell's value to an empty string. It returns True for a blank cell, for a cell containing only spaces typed by the user (not always, see the trimmed variant below), and for a formula that evaluates to"". - Understand what the empty-string test does not catch. A cell containing a single apostrophe or a real space is not equal to
"". If you need to treat whitespace as empty, testTrim(CStr(Range("A1").Value)) = "". Note thatCStron an empty cell gives an empty string, so the expression still works. - Try the IsEmpty test. Replace the condition with
If IsEmpty(Range("A1").Value) Then.IsEmptyis True only when the Variant holds no value at all, which is exactly the state of a never-touched cell. A cell with a formula returning""is not empty by this test, because the cell contains a formula. That distinction matters when you are deciding whether to overwrite data. - Use the safest combined check for data entry. When you want to skip anything a user would perceive as blank, combine both ideas:
If IsEmpty(cell.Value) Or Trim(cell.Value & "") = "" Then. Concatenating with""converts an empty Variant to an empty string, so theTrimpart never errors. - Loop over a range instead of single cells. To blank-check a whole column, declare a cell variable and loop:
Dim c As Range For Each c In Range("A1:A100") If Trim(c.Value & "") = "" Then c.Interior.Color = vbYellow End If Next cThis marks every blank in the range so you can see the result immediately. Build the range withRange("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)if the list length varies. - Guard against an empty last cell.
Cells(Rows.Count, "A").End(xlUp)returns row 1 even when the column is completely empty, because row 1 is always returned when there is no data above. Check that the top cell is not blank before trusting the resulting range, or you may loop over a bogus row. - Decide how to act on the result. Common actions are skipping the row (
If Not IsEmpty(...) Then), writing a default value, highlighting the cell, or exiting the sub withExit Suband a message. Pick one per check rather than nesting many conditions. - Test on known data. Put a value in A1, delete it, type a space, type
=""as a formula, and run your sub after each change. Watching the message box change is the fastest way to confirm which test you actually need. - Save the workbook. Press Ctrl+S and accept the prompt to keep the macro-enabled format. If you are offered .xlsx, accept .xlsm instead or the code is lost on close.
Common Mistakes
Comparing against Nothing. If Range("A1").Value = Nothing raises a type mismatch error, because an empty cell's value is a Variant of no value, not an object. Use "" or IsEmpty.
Testing the Range object instead of its value. If Range("A1") = "" works only because of a default-property shortcut, and it becomes confusing with multi-cell ranges, where the result depends on the top-left cell. Always write .Value explicitly.
Forgetting that a space is not empty. Cells that look blank but contain a space pass both tests. Trim(...) = "" is the fix, and it is also what catches imported data with trailing spaces.
Leaving off Option Explicit. Without Option Explicit at the top of the module, a typo such as cel.Value becomes a silently unassigned variable and your loop appears to do nothing. Turn it on in Tools > Options in the editor so new modules get it automatically.
Looping over the wrong range after the last row moves. A hard-coded A1:A100 stops working the moment data grows. Use the End(xlUp) pattern, or a named table whose range resizes itself.
Tips
Cache the range object in a variable once rather than repeating Range("A1") in every test; it is faster in long loops and easier to move to another sheet later.
Use IsError or IsNumeric alongside the blank test when a cell can hold an error such as #N/A, because an error is neither empty nor "".
If you are checking a whole row or column, worksheet functions such as COUNTBLANK can give the count in one line: WorksheetFunction.CountBlank(Range("A1:A100")). That counts formulas returning "" as blank, which may or may not suit you.
Write results into a message box or the Immediate window with Debug.Print while developing, then delete those lines before the sub goes to other users.
Troubleshooting
The message box never appears. Check where the code lives. A sub in a sheet module runs in that sheet's context; one in a standard module runs anywhere. Also confirm you pressed F5 from inside the sub, not from the wrong module.
Type mismatch error. You are probably comparing an object to a value or using a string function on a cell holding an error. Wrap the value in CStr(...) only after confirming it is not an error, or check IsError first.
The check says a formula cell is not empty. That is correct behaviour: the formula exists. If you need "looks blank," test the result string instead of IsEmpty. If you need "truly blank," clear the cell contents.
The macro is gone next time. The file was saved as .xlsx. Save a copy as .xlsm; content is not lost, only the code.
Macros are blocked. Excel shows a security warning bar. Only enable macros in workbooks you created or trust. Do not lower security settings to run code from an unknown source.
Frequently Asked Questions
What is the difference between IsEmpty and = "" in VBA?
IsEmpty is True only for a cell that holds no value at all. = "" is also True for a formula returning an empty string. Choose based on whether a formula-backed blank should count.
How do I check if an entire range is empty?
Use If WorksheetFunction.CountA(Range("A1:C10")) = 0 Then. CountA counts any non-empty cell, so a zero means every cell is empty.
Why does a cell with a space pass my empty test?
Because a space is a real character. Use Trim(cell.Value & "") = "" to treat whitespace-only cells as empty.
Can I check an empty cell without a macro?
Yes. Typing =ISBLANK(A1) in a worksheet does the same job, though ISBLANK treats a formula returning "" as not blank. Conditional formatting with the rule =A1="" colours apparent blanks instead.
What does End(xlUp) return for an empty column?
Row 1, because it always returns at least the starting boundary. Check whether cell A1 itself is blank before relying on that row number.
Does this work in Excel for the web?
No. The VBA editor is a desktop feature. Run and edit macros in Excel for Windows or Mac, then open the file in the browser only if you are not relying on the code there.