How to Check If Cell Is Empty Using Excel VBA (5 Easy Methods)

Get FREE Advanced Excel Exercises with Solutions!

Sometimes we need to check if there are any empty data in our large dataset in Excel. Implementing VBA macro is the most effective, quickest, and safest method to run any operation in Excel. In this article, we will show you how to check if a cell is empty in Excel using the VBA.

overview of using excel vba to check if multiple cells are empty

Here, the above screenshot shows the use of VBA to check an empty cell in Excel. I have used a VBA code to check the empty cell in Excel.

However, this section will discuss in detail how to check if one cell or multiple cells are empty in Excel by utilizing VBA macro. For the purpose of demonstration, I have used the following sample dataset which contains some fruit names and I have shown 5 different methods to make you understand the scenario properly.

sample dataset


1. Using the VBA IsEmpty Function to Check If Cell Is Empty 

From the dataset, we will find out how to check if Cell B9 is empty or not with VBA. Hence, the steps to check if one cell is empty in Excel by VBA are given below.

Steps:

  • Firstly, press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

open developer option

  • In the pop-up code window, from the menu bar, click Insert -> Module.

insert module in vba

  • Secondly, copy the following code and paste it into the code window.
Sub CheckEmptyCell()
    'declare object variable to hold reference to cell you work with
    Dim myCell As Range
    'identify cell you work with
    Set myCell = ThisWorkbook.Worksheets("Empty Cell").Range("B9")
    'check if cell is empty. Depending on result, display message box indicating whether cell is empty (True) or not empty (False)
    If IsEmpty(myCell) Then
        MsgBox myCell.Address & " is empty"
    Else
        MsgBox myCell.Address & " is not empty"
    End If
End Sub

Your code is now ready to run.

VBA to Check If One Cell is Empty in Excel

  • Thirdly, press F5 on your keyboard, or from the menu bar select Run -> Run Sub/UserForm. However, you can also just click on the small Play icon in the sub-menu bar to run the macro.

run vba code in excel

  • Finally, it will give you a message indicating whether your selected cell is empty or not (in our case, Cell B9 is empty so it showed us $B$9 is empty).

dialog box showing empty cell


2. Highlighting Empty Cells with Excel VBA Macro

Afterwards, consider the following dataset which has multiple empty cells in it and we will extract those out with VBA. Here, I have modified the previous dataset a bit.

checking multiple empty cells

The steps to inspect whether multiple cells are empty or not in Excel using VBA are shown below.

Steps:

  • Similarly, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • In the code window, copy the following code and paste it.
Sub CheckMultipleEmptyCells()
Dim i As Long
Dim c As Long
Dim myRange As Range
Dim myCell As Range
Set myRange = Range("B5:B15")
For Each myCell In myRange
    c = c + 1
    If IsEmpty(myCell) Then
        myCell.Interior.Color = RGB(255, 87, 87) 'use this line if you want
        'to highlight empty cells, otherwise skip it
        i = i + 1
    End If
Next myCell
MsgBox "There are total " & i & " empty cells out of " & c & "cells."
End Sub

Your code is now ready to run.

VBA to check If Multiple Cells are Empty in Excel

  • Run the macro.
  • Lastly, it will show you how many empty cells are in your dataset and highlight the empty cells for you.

result of VBA to check If Multiple Cells are Empty in Excel

Read More: How to Find & Count If a Cell Is Not Blank


3. Checking If Any Cell in a Range Is Empty with Excel VBA

Furthermore, we will check if there is any single empty cell present in a range in Excel with VBA. So, the steps to examine if there is an empty cell in a range in Excel using VBA are shown below.

Steps:

  • Similarly, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • In the code window, copy the following code and paste it.
Sub CheckEmptyCellInRange()
    'declare object variable to hold reference to cell range you work with
    Dim myCellRange As Range
    'identify cell range you work with
    Set myCellRange = ThisWorkbook.Worksheets("Cell in Range").Range("B5:B15")
    'check if number of non-empty cells in range is less than total number of cells in range. Depending on result, display message box indicating whether cell range contains any empty cell (True) or not (False)
    If WorksheetFunction.CountA(myCellRange) < myCellRange.Count Then
        MsgBox myCellRange.Address & " contains at least 1 empty cell"
    Else
        MsgBox myCellRange.Address & " doesn't contain empty cells"
    End If
End Sub

Your code is now ready to run.

VBA to Check If Any Cell in a Range is Empty in Excel

  • Run the code.
  • Finally, it will show you whether the range contains any empty cells or not (in our case, there was an empty (Cell B9) in the range B5:B15, so it showed us the message shown in the picture).

result of VBA to Check If Any Cell in a Range is Empty in Excel


4. Applying Excel VBA Macro to Inspect If Active Cell Is Empty 

Additionally, we learned how to check if a cell is empty or not, and now we will learn how to check if the active cell is empty or not in Excel with the VBA. Hence, the steps to determine if the active cell is empty in Excel by VBA are given below.

Steps:

  • Similarly, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • In the code window, copy the following code and paste it.
Sub CheckIfActiveCellEmpty()
 'check if active cell is empty. Depending on result, display message box indicating whether active cell is empty (True) or not empty (False)
    If IsEmpty(ActiveCell) Then
        MsgBox "The active cell is empty"
    Else
        MsgBox "The active cell is not empty"
    End If
End Sub

Your code is now ready to run.

VBA to check If Active Cell is Empty in Excel

  • Run the macro.
  • Lastly, it will tell you whether your active cell is empty or not (in our case, the active cell has the value Lemon so it shows the message of The active cell is not empty).

vba dialog box showing an active cell is not empty


5. Checking If All Cells in a Range Are Empty with VBA 

Now, we will check if all the cells of the range are empty or not by implementing VBA in Excel. For this reason, I have changed the dataset.

check all empty cells in range

The steps to check if all cells in a range are empty in Excel by VBA are given below.

Steps:

  • Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • In the code window, copy the following code and paste it.
Sub EmptyCellRange()
Dim cell As Range
Dim bIsEmpty As Boolean
bIsEmpty = False
For Each cell In Range("B5:B15")
    If IsEmpty(cell) = True Then
        bIsEmpty = True
        Exit For
    End If
Next cell
If bIsEmpty = True Then
    MsgBox "All cells are empty in your range!"
Else
    MsgBox "Cells have values!"
End If
End Sub

Your code is now ready to run.

VBA to Check If All Cells in a Range are Empty in Excel

  • Run the macro.
  • Finally, this will give you the message according to the value of your cells in the range (in our case, we had no value inserted inside the range B5:B15 so we got the message shown above in the message box).

vba output to show all cells are empty

Read More: Excel VBA: Check If Multiple Cells Are Empty


Download Practice Workbook

You can download the free practice Excel template from here.


Conclusion

This article showed you how to check if a cell is empty in Excel by implementing the VBA macro. I hope this article has been very beneficial to you. Feel free to ask any questions regarding the topic.


Related Articles


<< Go Back to If Cell is Blank Then | Blank Cells | Excel Cells | Learn Excel

What is ExcelDemy?

ExcelDemy - Learn Excel & Get Excel Solutions Center provides online Excel training , Excel consultancy services , free Excel tutorials, free support , and free Excel Templates for Excel professionals and businesses. Feel free to contact us with your Excel problems.
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo