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.
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.
Download Practice Workbook
You can download the free practice Excel template from here.
5 Easy Methods on Implementing VBA to Check If Cell Is Empty 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.
1. Excel VBA to Check If One 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.
- In the pop-up code window, from the menu bar, click Insert -> Module.
- 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.
- 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.
- 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).
2. VBA Macro to Examine If Multiple Cells Are Empty in Excel
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.
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.
- Run the macro.
- Lastly, it will show you how many empty cells are in your dataset and highlight the empty cells for you.
3. Check If Any Cell in a Range Is Empty Through 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.
- 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).
Read More: How to Check If Value Exists in Range in Excel (8 Ways)
4. 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.
- 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).
5. VBA to Check If All Cells in a Range Are Empty
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.
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.
- 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).
Read More: How to Turn on Spell Check in Excel (3 Ways)
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.