To check if a cell is empty, you can use VBA’s ISEMPTY function. In this function, you need to use the range object to specify the cell you want to check, and it returns true if that cell is empty; otherwise false. You can use a message box or a cell to get the result.
Use VBA to Check IF a Cell is Empty
- Start with the function name “IsEmpty”.
- Specify the cell that you want to check.
- Use a message box or a cell to get the result value.
- In the end, run the code.
MsgBox IsEmpty(Range("A1"))
Check IF Multiple Cells Empty
If you want to check and count the empty cells from a range when you need to loop through each cell in the range.
Sub vba_check_empty_cells()
Dim i As Long
Dim c As Long
Dim myRange As Range
Dim myCell As Range
Set myRange = Range("A1:A10")
For Each myCell In myRange
c = c + 1
If IsEmpty(myCell) Then
i = i + 1
End If
Next myCell
MsgBox _
"There are total " & i & " empty cell(s) out of " & c & "."
End Sub
The above code loops through each cell in the range A1:A10 and checks each cell individually using the ISEMPTY function to determine whether it’s empty or not. It counts each empty cell and, in the end, shows a message box with the total number of cells and empty cells.
Use the following code if you want to highlight empty cells as well.
Dim i As Long
Dim c As Long
Dim myRange As Range
Dim myCell As Range
Set myRange = Range("A1:A10")
For Each myCell In myRange '
c = c + 1
If IsEmpty(myCell) Then
myCell.Interior.Color = RGB(255, 87, 87)
i = i + 1
End If
Next myCell
MsgBox _
"There are total " & i & " empty cell(s) out of " & c & "."