Search code examples
excelvba

Test three non-contiguous ranges to determine if all cells are empty


I need to check three non-contiguous ranges to determine if they have been populated with dates.
If not, return a message informing the user.

What I have debugs with

run-time error '424': Object required

at "If Range("T2")...

Sub DateChecker()

Dim tlastRow As Long
Dim ulastRow As Long
Dim wlastRow As Long

tlastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ulastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
wlastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

If Range("T2" & tlastRow) Is Empty And Range("U2" & ulastRow) Is Empty And Range("W2" & wlastRow) Is Empty Then GoTo NoDates
NoDates:
MsgBox "Dates have not been populated", vbExclamation
Exit Sub

End Sub

Solution

  • Is Empty is not used in VBA in this context. Furthermore IsEmpty function cannot be used for ranges with more than one cell.

    Try to use WorksheetFunction.Counta function sg. like this

    If WorksheetFunction.Counta(Range("T2:T" & tlastRow))=0 And _ 
    WorksheetFunction.Counta(Range("U2:U" & ulastRow))=0 And _ 
    WorksheetFunction.Counta(Range("W2:W" & wlastRow))=0 Then
        MsgBox "Dates have not been populated", vbExclamation
    End If
    
    
    End Sub
    

    You will get the message only if all of the ranges are totally empty.