Smart Systems for Home

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

Excel VBA Check If String Is Empty

· 5 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

You can check if a string variable is empty in Excel VBA using the Len() function combined with an If statement, or by comparing the string directly to an empty string literal using strVar = "". Both methods return a Boolean value (True or False) that determines whether the code block should execute.

What You'll Need

  • Microsoft Excel installed on a Windows PC (VBA is not available on Mac in the same way).
  • A worksheet or workbook where you want to run the code.
  • Basic familiarity with the Excel VBA Editor (Alt + F11).

Step-by-Step

  1. Open the VBA Editor: Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications window. This is where you will write your code.
  2. Insert a Module: In the VBA Editor, go to the menu bar and select Insert > Module. This creates a new code window where you can type your script.
  3. Declare a String Variable: In the new module window, type the following line to declare a variable that will hold a string. This example uses a public variable so it can be accessed from other parts of your workbook, but you can also use a local variable inside a Sub procedure.
    Public myString As String
  4. Assign a Value (Optional): To test the empty check, you need a string to work with. You can assign a value manually or pull data from a cell. Add this line to your code:
    myString = "Hello World"
  5. Write the Empty Check: Now, write the If statement to check if the variable is empty. The most common method is using the Len() function, which counts the number of characters. An empty string has a length of 0.
    If Len(myString) = 0 Then
        MsgBox "The string is empty."
    Else
        MsgBox "The string is not empty."
    End If
  6. Add a Second Check (Alternative Method): You can also check for emptiness by comparing the variable directly to an empty string. This is often considered more readable by some programmers.
    If myString = "" Then
        MsgBox "The string is empty."
    Else
        MsgBox "The string is not empty."
    End If
  7. Run the Code: Close the module window and return to your Excel spreadsheet. Press Alt + F8 to open the Macro dialog box. Select the macro you want to run from the list and click Run.
  8. Test Different Scenarios: To fully understand how this works, change the value of myString in your code to an empty string (just leave it blank) or a variable that comes from a cell (like myString = Range("A1").Value). Run the code again to see the different messages.

Common Mistakes

One of the most frequent errors when checking for empty strings is confusing a NULL value with an empty string. In VBA, NULL means the variable has not been assigned a value at all and is effectively empty, but you cannot use the Len() function on a NULL value without first converting it.

Another common mistake is checking for a string that contains only spaces. A string with spaces is technically not empty because it contains characters. The Len() function will return a number greater than 0 for a string like " ". To handle this, you must use the Trim() function to remove leading and trailing whitespace before checking the length.

Tips

When working with data imported from Excel cells, it is best practice to check the cell value directly rather than relying on a variable you assigned earlier. This ensures you are checking the current state of the data on the sheet. For example, you can check if cell A1 is empty by using If Len(Range("A1").Value) = 0 Then.

For more robust data cleaning, combine the length check with the Trim() function. This allows you to treat a string that is empty, a string that is full of spaces, or a string that is full of invisible characters as truly empty. The updated code would look like this:

If Len(Trim(myString)) = 0 Then
    MsgBox "The string is empty or only contains whitespace."
Else
    MsgBox "The string has content."
End If

Troubleshooting

If your code is not behaving as expected, the first thing to check is the data type of the variable. If you declare a variable as an Integer or a Double but try to treat it as a String, the VBA engine may automatically convert it, or the If statement may fail to recognize the empty state correctly. Always declare your variables as String when checking for text content.

If you are checking a range of cells and the macro stops with a runtime error, it is likely because the code is trying to read a cell that is empty. You can prevent this error by wrapping the length check in an On Error Resume Next statement, although this is generally discouraged for production code as it hides other errors. A better approach is to check if the cell is empty before proceeding.

Frequently Asked Questions

What is the difference between "" and NULL?

An empty string ("") is a string variable that has been assigned a value of zero characters. NULL means the variable has not been assigned any value at all and is in an undefined state. You can check for an empty string, but you cannot use functions like Len() on a NULL variable without risking an error.

Why does Len(myString) return 1 if myString is a space?

A space is a valid character in VBA. The Len() function counts every character, including spaces, tabs, and line breaks. To treat a string with only spaces as empty, you must use the Trim() function to remove those whitespace characters first.

Can I check if a cell is empty in VBA?

Yes. You can check an Excel cell directly by referencing its address. The code If Len(Range("A1").Value) = 0 Then will evaluate to True if cell A1 is empty.

Is there a built-in function to check for empty strings?

No, VBA does not have a dedicated function like IsEmpty() for strings. IsEmpty() is designed to check if a variable of type Variant is uninitialized. For strings, you must use the Len() function or a direct comparison (str = "").

How do I handle a string that might be NULL?

You can use the IsNull() function to check for a NULL value. If you want to treat both empty strings and NULL values as empty, you can combine the checks: If Len(myString) = 0 Or IsNull(myString) Then.