Identify Empty Cells in VBA

Whenever I need to determine if a cell is blank, I call the IsEmpty function on the Value property.

If IsEmpty(ActiveCell.Value) Then

This anonymous blogger uses vbNullString.

If ActiveCell.Value = vbNullString

In other news: In Firefox, Cntl+Click on a hyperlink and it will open that link in a new tab. Except for the aforementioned blog’s heading. When I Cntl+Click on the blog’s header link, it opens a new tab and it follows the link in the current tab. It seems to work normally for any other link on that site. I’ve never seen that behavior before.


  1. Eric says:

    Without bothering to check, it’s probably some Javascript.

  2. Quinn says:

    In Firefox, clicking the middle mouse button will also open a link in a new tab.

  3. Jason says:

    In Firefox, click on links with your middle-mouse button to open in a new tab. But, your suggestion is news to me and very helpful when using a laptop!

  4. jgluck says:

    Is there any way create an empty (null) cell? Specifically, suppose you’re doing a calculation based on the presence/absence of data in another cell; something like =if(len(othercell)=0,”",do_calc).
    Rather than entering “”, can one make it an empty cell, e.g., if(len(othercell)=0, make_null, do_calc).

    The reason why “” is sometimes a nuisance is that cells like this can show in a pivot table as 0 instead of empty. Of course you could filter the original calculated column for blanks, then do a “clear all”. Another way I found recently was a sub where you’d select a range, then loop thru, something like: For each cell in selection, If len(Trim(cell)) = 0 Then cell = empty or cell.clear. However, I found this to run very slowly. So what would be nice to have is the ability to create empty/null cells on the fly. Or have I missed something simple (mea culpa)?

  5. Good call Eric. I hadn’t thought of that.

  6. Hui... says:

    A central mouse button click (assuming you have 3 mouse buttons) has always opened links in another Tab in all versions of Firefox ?

  7. MacroMan says:

    vbNullString, never used that one…thanks.

  8. This is last covered at

    vbNullString is the same as “”
    ActiveCell.Value = vbNullString
    will return True for formulas that evaluate to “”
    That may be intentional but it’s not empty.

    If a cell contains an apostrophe prefix but is otherwise blank that also returns True but IsEmpty() does not.

    In formulas, use =ISBLANK()
    In VBA, use IsEmpty()


  9. MoveFirst says:

    In answer to jdluck’s query above, a quick test seems to indicate that vbNullString will only work in VBA and not when used with the IF function within a formula.

    It seems that for 99% of the time vbNullString is the same as “” (empty string). However, if we start using unicode or pointers then things get complicated.

    If we really wanted to follow jdluck’s example and create a make_null function we could do the following:

    1) Create a new VBA function something like this:

    Function Make_Null()
    Make_Null = vbNullString
    End Function

    2) Call this from your IF statement (e.g.


    This seems to do what we’re looking for. Comments welcome.

    Lastly, thanks to Daily Dose of Excel for linking to my new (and still rather empty) blog!

  10. Rob Bovey says:

    vbNullString is not quite the same thing as “”, although they behave identically in VBA for all practical purposes. Under the covers, though, vbNullString is exactly what it says – a Null string. To see the difference, open the Immediate Window and compare the results of these two:

    ? StrPtr(“”)
    ? StrPtr(vbNullString)

    The first is an allocated string that just happens to have a length of zero, the second is a string with no memory allocated.

    The only time I’ve ever seen this difference become important as a practical matter is when dealing with API calls that expect a pointer to a null string, which you can only get in VBA using vbNullString.

  11. mrt says:

    Hi Patrick -

    This depends if you need what the computer knows, or the eye sees. A cell with empty text within looks blank, but ISBLANK(cell) is false, and the answer disagrees with your eyes. In most such cases, I test if LEN(cell) = 0, in formula or VBA, which brings the computer and my eyes into agreement.


  12. Jon Peltier says:

    @jgluck: “Is there any way create an empty (null) cell?”

    No. We have suggested numerous times that MS add something like a NULL() or BLANK() worksheet formula that would act as a truly blank cell. This would help with downstream calculations, charting, and presentation. We’re still waiting.

  13. Jon Peltier says:


    Make a line or XY chart using Make_Null in place of blank cells. A cell containing a formula returning Make_Null is treated as if it has a value of zero. A cell that is truly blank behaves differently, resulting in either a gap where the cell’s value would be plotted, or a line interpolated across the gap. There is no formulaic way to simulate a cell that is truly blank.

  14. MoveFirst says:

    It seems we’re all saying the same thing here but in different ways. On reflection I agree with Jon Peltier in that “there is no formulaic way to simulate a cell that is truly blank”.

    I came across this issue using VBA when a cell (formatted as a date) would not return either IsEmpty, equal to NULL or even as a number. In that case the vbNullString solved the problem.

    Rob Bovey’s comment above reminded me of some Win32 API programming in VB that I’d done years ago and I came across the following definition of vbNullString (now, I also note, repeated in the Office 2007 help file):

    vbNullString – String having value 0 – Not the same as a zero-length string (“”); used for calling external procedures

    As a VBA (or VB) string is only a pointer to a character array. The address in memory pointed to by this variable points to the start of the character array.

    The array terminates in a 2-byte null terminator and is preceded by 4 bytes showing the length of the string in bytes.

    Using “” to declare an empty string results in a character array of size 0. However, this will still contain the 4-byte length data, and the 2-byte terminator.

    vbNullString still returns a pointer but one that does not point to any character array resulting in no 4-byte length field or 2-byte terminator.

    An article by Microsoft (of which I have a printed copy but now can’t find the URL) on VBA optimization suggests that vbNullString would execute 50% faster than “”.

    Having said all that I return to my original comment – use what works for you in the situation you’re faced with. vbNullString saved the days for me so may help others.

  15. Simon Murphy says:

    can you create a null cell (as a function result)?
    no I don’t think so, by definition, by putting something in it (the formula) you are changing it from a null cell to some other type. That seems fair enough to me. =NA() is the next best thing for graphs etc.

    If you enter =A2 in A1 where A2 is empty A1 returns 0. Excel coerces that empty cell to a number of value zero. If it didn’t, blanks would propagate all over and break select special stuff for example.

    If you change A2 to be =”" then A1 looks blank, but its value is really a zero length string. If you paste the value over A2 you get a cell that looks and feels blank, nothing in the formula bar etc. But it is not a blank cell its a string cell with a zero length string. This means its not ISBLANK() and its not isEmpty() but it is the equivalent of vbNullString.

    I tend to use if len(cl.value)=0 then…

  16. Jon Peltier says:

    If you are testing for a blank in VBA, this is faster:

    If Len(rCell.Value) = 0 Then

    than this:

    If rCell.Value = “”

    because VBA determines the length of a string as the first step in getting the string itself. That doesn’t necessarily help with rCell.Value, which trips out to the worksheet, but it does help with a string variable in memory and it also helps with “”.

  17. MacroMan says:

    totally off topic, but I just picked up a VBA book I haven’t touched in a while and I learned a something new…
    the Evaluate() method.

    instead of:
    Dim n as Long

    n = worksheetfunction.Sum(sheet1.[A1:A2].cells)

    Msgbox n

    you can write:

    MsgBox Evaluate(“Sum(A1:A2)”)

  18. Jon Peltier says:

    It’s best to use Evaluate, as well as the [] modifiers, only in the Immediate window. For one thing, I’ve heard that the brackets are slower than the normal VBA referencing. For another, and more important, unless you are sure that the active sheet contains the cells you want to sum, you will not get what you expect.

  19. Hans Schraven says:

    As Evaluate seems to be the equivalent of the use of brackets I’d think this will do better

    MsgBox WorksheetFunction.Sum([Blad3!A1:A3])

    It’s possible to point at the sheet between brackets; Jon’s second argument is therefore irrelevant.
    I’ve never been able to establish any speed difference in using/not using brackets.

  20. Hans Schraven says:

    The result of continued testing:

    MsgBox Evaluate(“Sum(Blad3!A1:A3)”)

    None of Jon’s objections apply to this code.

  21. Hui... says:

    This post has been a lot of talk over nothing !

  22. MacroMan says:

    So let me get this straight, no difference in performance in using [Sheet1!A1:A2] vs Sheet1.Range(“A1:A2?)?

  23. Jon Peltier says:

    Hans – TouchĂ©!

  24. Sorry for my English..

    ‘True for really empty
    CellIsReallyEmpty = IsEmpty(ActiveCell)
    ‘True for ;;; or “”
    ‘respect zero hidding
    CellForEyesEmpty = Len(ActiveCell.Text) = 0

    And yes, vbNullsString is not the same as zero lenght string “” (storage difference, string pointers)

  25. And my tip for “first empty cell from bottom”:

    ColumnNumber = 1<br>
    With Cells(Rows.Count, ColumnNumber).End(xlUp)<br>
    .Offset(Sgn(.Row - 1), 0).Select<br>
    End With

  26. MacroMan says:

    Can anyone explain this code:

    Sub c()
    Dim v As Variant, i As Long, j As Long
    v = Range(“A1:Z10000?).Value
    For i = LBound(v, 1) To UBound(v, 1)
    For j = LBound(v, 2) To UBound(v, 2)
    v(i, j) = Trim(v(i, j))
    Range(“A1:Z10000?).Value = v
    End Sub

    Why does the variable “v” have parameters??

  27. Stephanie Smith says:


    v is being used to hold an array rather than a single variable. The code is using i and j to iterate through the rows and columns of the array and Trim (remove trailing spaces from) each item in it. Then it writes the contents of v (the entire array) back out to the range that it got it from.

  28. MacroMan says:

    ahhhhh, I think the Variant data type confused me. It could have also been “Dim v() as Variant”. That would have been clearer. I forget this language is not so consistent. Thanks Stephanie!


  29. Kronemab says:

    I’m running into a similar issue w/empty cells, in that I’m trying to copy one column of data from one sheet to another.

    However, the column in the first sheet may or may not have blank cells (identified by “”), depending on parameters set by user – and what I’d like to do is set up code that always pastes the actual data in consecutive rows.


    First Sheet

    1 12344
    2 “”
    3 “”
    4 4525
    5 4565
    6 “”

    Second Sheet (desired output)

    1 12344
    2 4525
    3 4565

    Any ideas?

  30. Simon Murphy says:

    Wilmott by any chance??

  31. MacroMan says:


    Whoa, how in the world did you know that!? Yes it is, the no “()” in the variant Dim statement confused me. I read your blogs, but I haven’t been playing around with VBA code in a while.

  32. Geoff K says:

    And going back to MacroMan question on variant arrays they are impressively quicker too as this demo will show:

    Sub TryThis()

        Dim rng As Range
        Dim tbl As Range
        Dim tbl2 As Variant
        Dim start As Double
        Dim i As Long, j As Long
        Dim arr(0 To 19999)

        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual

        ”’set up some work
        Set tbl = Range(“A2:A20000″)
        For Each rng In tbl
            rng = ” as range “
            rng.Offset(, 3) = ” as array “
            rng.Offset(, 6) = ” as varnt “

        ”’as range object
       Set tbl = Range(“A2:A20000″)
        start = Timer
        For Each rng In tbl
            rng.Offset(, 1) = Trim(rng)
        Debug.Print “as range”, Timer – start

        ”’as array
       Set tbl = Range(“D2:D20000″)
        start = Timer
        For i = 0 To 19999
            arr(i) = Cells(i + 2, “D”)
        For i = 0 To 19999
            Cells(i + 2, “E”) = Trim(arr(i))
        Debug.Print “as array”, Timer – start

        ”’as variant array
       tbl2 = Range(“G2:G20000″)
        start = Timer
        For j = LBound(tbl2, 1) To UBound(tbl2, 1)
            tbl2(j, 1) = Trim(tbl2(j, 1))
        Next j
        Range(“H2:H20000″) = tbl2
        Debug.Print “as varnt”, Timer – start

        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic

    End Sub

  33. MacroMan says:

    Thanks Geoff, like I said I haven’t been playing with VBA in a while. It would have been clearer if the Dim statement said “v() as Variant”, note the “()”. Here is the same method in C# on a Form:

    private void button1_Click(object sender, EventArgs e)
    foreach (Control ctl in this.Controls)
    string ctltype = ctl.GetType().ToString();
    if (ctltype == “System.Windows.Forms.TextBox”)
    ctl.Text = ctl.Text.Trim();

  34. James Taylor says:

    Your firefox issue seems to be a Javascript – many websites use javascript on “a” tags to do … things. When you control click, it opens as a new tab AND runs the javascript. A tags should have href (or name) variables to support web browsers which don’t have Javascript (and also, search bots which might not understand JS).

    Bascially, another example of Javascript being evil because someones written something evil in it…

  35. Tom, Ogilvy says:

    Re: Macroman, Hans Schraven and Peltier discussion on Brackets.
    Microsoft had a KB article which stated that using brackets for referencing ex: [A1:A10] (what theycalled tunneling as I recall) is slower than using references such as Range(“A1:A10?). This is for referencing ranges such as [A1:A10].Value = 1. I can’t say for executing a worksheet function such as [Sum(A1:A10)]

  36. MacroMan says:

    Good to know Tom, thanks.

  37. Jamie Collins says:

    @Tom: Square Bracket Notation Is Less Efficient Than Tunneling

  38. Callum Donez says:

    Brilliant – 1 line of code rather than hours of looping etc

  39. Donahu says:

    vbNullString is handy when you need to test if a cell is empty, because my macro was not making the difference between 0 and an empty cell.

    This sucks ;d

  40. C Norman says:

    Use four quotes for If() Statements inserting into cells:

    Sheets(“summary report”).Range(“p6″).FormulaR1C1 = “=IF(RC[-1]0,RC[-1]*RC[1],”"”")”

    The above formula successfully achieves a blank cell shown in Excel when the if statement is false.

Posting code or formulas in your comment? Use <code> tags!

  • <code lang="vb">Block of code goes here</code>
  • <code lang="vb" inline="true">Inline code goes here</code>
  • <code>Formula goes here</code>

Leave a Reply

Here's how to update your reports of company and nearly any web data: