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

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

Steps:

  • 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

  • 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

  • Press F5 on your keyboard, or from the menu bar, select Run -> Run Sub/UserForm. Click on the small Play icon in the sub-menu bar to run the macro.

run vba code in excel

  • It will give you a message indicating whether your selected cell is empty (Cell B9 is empty so it showed us $B$9 is empty).

dialog box showing empty cell


Method 2 – Highlighting Empty Cells with Excel VBA Macro

Steps:

  • 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.
  • 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


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

Steps:

  • 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.
  • It will show you whether the range contains any empty cells (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


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

Steps:

  • 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.
  • 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


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

Steps:

  • 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.
  • This will give you the message according to the value of your cells in the range (in our case, no value was 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


Download Practice Workbook

You can download the free practice Excel template from here.


Related Articles


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

Get FREE Advanced Excel Exercises with Solutions!
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