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.
- In the pop-up code window, from the menu bar, click Insert -> Module.
- 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.
- 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.
- 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).
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.
- Run the macro.
- It will show you how many empty cells are in your dataset and highlight the empty cells for you.
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.
- 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).
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.
- 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).
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.
- 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).
Download Practice Workbook
You can download the free practice Excel template from here.
Related Articles
- If Cell is Blank Then Show 0 in Excel
- How to Return Value If Cell is Blank
- How to Calculate in Excel If Cells are Not Blank
- How to Apply Conditional Formatting in Excel If Another Cell Is Blank
- If a Cell Is Blank then Copy Another Cell in Excel
- Excel If Two Cells Are Blank Then Return Value
<< Go Back to If Cell is Blank Then | Excel Cells | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!