Excel VBA: Check Whether a String Is Empty or Blank

VBA & macros · Updated November 3, 2026

Excel VBA: Check Whether a String Is Empty or Blank

In Excel VBA, use Len(text) = 0 to check whether a string has no characters. If you also want to treat spaces as blank, test Len(Trim$(text)) = 0; the right choice depends on whether whitespace should count as content.

What “empty” means in VBA

A zero-length string is a string containing no characters. In VBA, it can be written as "" or represented by the built-in constant vbNullString. Both are suitable values to compare with a String variable. For example, a variable assigned vbNullString has a length of zero.

“Empty” can also refer to a different VBA value: the special Empty value of a Variant that has not yet been assigned a value. This distinction matters when a macro reads worksheet cells or works with variables declared as Variant. A String variable cannot hold the Variant value Empty; it holds string data, including a zero-length string.

Blank-looking text is another case. A string containing one space has a length of one, even though it may look empty in a cell or message. Tabs, line breaks, and other whitespace characters are also characters. Decide whether your rule means “no characters at all” or “nothing visible after trimming” before writing the condition.

The simplest check with Len

For a variable declared As String, Len(text) = 0 is a direct and readable test. Len returns the number of characters in the string, so the condition is true for a zero-length string and false for a string containing any character.

Sub CheckString()
    Dim textValue As String

    textValue = vbNullString

    ' A zero length means the string has no characters.
    If Len(textValue) = 0 Then
        MsgBox "The string is empty."
    Else
        MsgBox "The string contains text."
    End If
End Sub

Before running a macro, save a copy of the workbook: macro actions cannot be undone with Ctrl+Z. This sample only displays a message, but the same check is often used before code writes values, builds a report, or skips incomplete records.

You can also write textValue = vbNullString or textValue = "" as a test. Those comparisons are valid for a String variable, but Len(textValue) = 0 makes the length-based intent especially clear. Pick one style and use it consistently.

Choosing between Len, an empty-string comparison, and IsEmpty

These tests are related, but they do not answer exactly the same question:

  • Len(text) = 0 checks whether a string has zero characters.
  • text = vbNullString checks whether a string equals the zero-length string.
  • IsEmpty(value) checks whether a Variant is still in its special Empty state.
  • Len(Trim$(text)) = 0 checks whether removing leading and trailing ordinary spaces leaves no characters.

A common mistake is to use IsEmpty on a String variable to look for an empty string. It is not the right test: IsEmpty is intended for Variants and tests the Variant’s state, not whether a string contains characters. For a declared String, use Len or compare it with vbNullString.

For a Variant, first decide whether you care about an unassigned value, an empty string, or both. An unassigned Variant can satisfy IsEmpty(value), while a Variant holding "" is a string value. The latter needs a string-length or string-comparison test.

How to treat spaces and other whitespace

Use Len(text) = 0 when spaces are meaningful and should make the test false. Use Len(Trim$(text)) = 0 when a value made only of ordinary leading or trailing spaces should count as blank. For example, a string containing three spaces has a length of three, but its trimmed version has a length of zero.

Trim$ removes ordinary spaces at the beginning and end of a string. It does not mean “remove every whitespace character”: a tab or line break may remain and keep the resulting length above zero. If your input can contain tabs or line breaks and those should also count as blank, remove or test for those characters explicitly according to your data rules. Do not trim automatically if spaces are significant, such as in a fixed-width code or text where the exact contents must be preserved.

Trimming for a check does not require changing the original value. In Len(Trim$(text)) = 0, VBA evaluates the trimmed result for the condition; it does not assign that result back to text. If you want to normalize the stored value too, assign the trimmed result deliberately and consider whether altering the source text is appropriate.

Checking a Variant safely

When a value can be an unassigned Variant, a string, or an error value read from a cell, test its type or state before using it as text. This function treats an unassigned Variant and a zero-length string as empty, but does not treat errors, Null, numbers, or strings of spaces as empty. It is intended for scalar values, not arrays or objects.

Function IsEmptyTextValue(ByVal value As Variant) As Boolean
    ' Errors and Null are not empty text.
    If IsError(value) Then Exit Function
    If IsNull(value) Then Exit Function

    ' An unassigned Variant is empty.
    If IsEmpty(value) Then
        IsEmptyTextValue = True
    ElseIf VarType(value) = vbString Then
        ' A string is empty only when it has zero characters.
        IsEmptyTextValue = (Len(value) = 0)
    End If
End Function

The function’s name describes its limited rule: it recognizes the special Empty value and zero-length text. It deliberately returns False for a string containing spaces. To treat spaces as blank too, change the string branch to use Len(Trim$(value)) = 0. Keep the separate checks for errors and Null if those values may reach the function; a worksheet error is not an empty string.

VBA’s And operator does not short-circuit. Avoid relying on a condition such as IsEmpty(value) Or Len(value) = 0 to protect a later expression from an incompatible value: VBA may evaluate both sides. Separate checks, as in the function above, make the order and the intended handling explicit.

Checking values read from worksheet cells

A cell that looks blank can produce different VBA values depending on its contents. A truly empty cell read through Range.Value2 is commonly represented as an Empty Variant. A cell containing a formula that returns an empty string, such as a formula whose result is "", instead supplies a zero-length string. A cell containing spaces supplies a nonempty string, and a cell with an Excel error supplies an error value.

For example, with cell A1 on Sheet1 as the value to inspect, read it into a Variant and branch on the cases before treating it as text:

Sub CheckCellA1()
    Dim cellValue As Variant

    cellValue = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value2

    ' Handle an Excel error before checking text.
    If IsError(cellValue) Then
        MsgBox "A1 contains an error value."
    ElseIf IsEmpty(cellValue) Then
        MsgBox "A1 is truly empty."
    ElseIf VarType(cellValue) = vbString Then
        If Len(cellValue) = 0 Then
            MsgBox "A1 contains zero-length text."
        Else
            MsgBox "A1 contains text."
        End If
    Else
        MsgBox "A1 contains a non-text value."
    End If
End Sub

Save a copy of the workbook before running a macro, because Ctrl+Z cannot undo VBA changes. This example only reads a cell and displays a message. If you adapt the pattern to change cells, test it on a copy first. Also note that Value2 returns values rather than formatting: a cell that appears blank because of its number format may still contain a number.

Common problems and how to avoid them

  • Using IsEmpty on a String: use Len(text) = 0 for a String variable. Reserve IsEmpty for checking a Variant’s Empty state.
  • Assuming spaces count as empty: they do not. Use Len(Trim$(text)) = 0 if ordinary spaces at the ends should be ignored.
  • Reading a formula result as a truly blank cell: a formula returning "" is text, not an unassigned cell. Check for both Empty and zero-length text if both cases matter.
  • Passing a worksheet error into a string-only check: handle IsError before applying string operations to a Variant from a cell.
  • Assuming VBA conditions short-circuit: split checks into ordered If or ElseIf branches when later expressions require a particular type.

Another subtle case is a value converted to text before checking. Converting a number or date with CStr produces text, but that text will not be empty merely because the original cell looked blank or was displayed unusually. Check the original Variant’s state and type when the distinction matters, rather than converting every input to a string first.

A practical rule for choosing the test

Choose the test that matches the decision your macro needs to make. For a declared String, Len(text) = 0 is the usual default. If users may type spaces and those should not count as an answer, test the trimmed length. If the input is a Variant from a cell, distinguish a genuinely empty cell, zero-length text, errors, and other value types before proceeding.

It can help to define the rule in plain language before writing code: “reject only strings with no characters,” “reject strings containing only ordinary spaces,” or “skip cells that are truly empty or return an empty string.” These rules are not interchangeable. A clear rule prevents a macro from discarding meaningful spaces or accepting a cell that only appears blank.

For larger procedures, put repeated checks into a small helper function with a name that states the rule, such as IsEmptyTextValue or IsBlankAfterTrim. A descriptive name makes later code easier to review. Keep the function’s behavior narrow and document whether it accepts spaces, tabs, errors, and Null values.

Frequently Asked Questions

How do I check whether a VBA string is empty?
Use If Len(myString) = 0 Then. This is true when the string contains no characters.

Is an empty string the same as Empty in VBA?
No. An empty string is text with zero characters. Empty is a special state for an unassigned Variant. Use Len for a String and IsEmpty when you need to test a Variant’s state.

How do I treat spaces as empty?
For ordinary spaces at the beginning or end, use Len(Trim$(myString)) = 0. Remember that tabs and line breaks are not removed by Trim$.

Does IsEmpty detect a cell containing a formula that returns “”?
No. That cell contains a formula result represented as a zero-length string, not an unassigned Variant. Check whether the cell value is a string and whether its length is zero.

Should I use vbNullString or “”?
Both can represent a zero-length string in a String comparison. Len(value) = 0 is another clear option and states directly that the test concerns character length.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.