I’ve checked so far and there are no question on 3 autofilter criteria with the error message of expression not defined.

Context: I’m making a vba of data range with 3 different set of data: Today, tomorrow and the day after tomorrow.

Previously, the code that I used for 2 criteria does not have any issue. But when I add a new criteria, I found a new error. Attached is my code and kindly let me know what can I improve and solve for this.

Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Dim rng As Range
Dim TodaysDate As Date
Dim TomorrowsDate As Date

'~~> Today's date
TodaysDate = Date
'~~> Tomorrow's date
TomorrowsDate = DateAdd("d", 1, Date)
'~~> After's Date
Aftersdate = DateAdd("d", 2, Date)

'~~> Set this to the relevant worksheet
Set ws = Worksheets("STG_SB_OPICS_DTL")

With ws
    '~~> Remove any existing filter
    .AutoFilterMode = False
    
    '~~> Find last row in Col D
    lRow = .Range("D" & .Rows.Count).End(xlUp).Row
    
    '~~> Identify your filter range
    Set rng = .Range("D3:D" & lRow)
    
    '~~> Filter
    With rng
        .AutoFilter field:=1, _
        Criteria1:="=" & TodaysDate, _
        Operator:=xlOr, _
        Criteria2:="=" & TomorrowsDate
        Criteria3:="=" & Aftersdate
    End With
End With

End Sub

New contributor

Dave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Khám phá các thẻ bài đăng