If you find yourself in a situation where you can’t use CountA
then it's much faster to first store your range as an array and loop on the array data than it is to loop on range/cell data.
Function IsRangeEmpty(ByVal rng As Range) As Boolean
'Converts a range to an array and returns true if a value is found in said array
Dim area As Range
For Each area In rng.Areas
If area.Cells.Count > 1 Then
'save range as array
Dim arr As Variant
arr = area.value
'loop through array
Dim cel As Variant
For Each cel In arr
'if cell is not empty then
If Len(Trim(cel)) > 0 Then
IsRangeEmpty = False
Exit Function
End If
Next cel
Else 'cannot loop on array with one value
'if cell is not empty then
If Len(Trim(area.Value2)) > 0 Then
IsRangeEmpty = False
Exit Function
End If
End If
Next area
IsRangeEmpty = True
End Function
Example of how to use it:
Sub Test()
Debug.Print IsRangeEmpty(Range("A38:P38"))
End Sub
If Range("A38:P38")
is empty, it would print True
in the Immediate Window; otherwise it'd print False
.