2

The following code filters a range of dates by the dates in August

ActiveSheet.Range("$A$29:$CG$3582").AutoFilter field:=18, Criteria1:= _
    xlFilterAllDatesInPeriodAugust, Operator:=xlFilterDynamic

How do I make this code dynamic so that I can filter the range by any month (e.g. month(today()))?

2 Answers 2

2

Try Criteria2, this is a bit cryptic though. Pass Array(1, datestring), where 1 will filter the entire month of that date, and the datestring must be in m/d/y format.
(0 = entire year, 2 = day, and some more, as discussed somewhere here).

Dim s As String
s = Format(Now, "MM""/""dd""/""yyyy")

ActiveSheet.Range("$A$29:$CG$3582").AutoFilter _
    Field:=18, Operator:=xlFilterValues, _
    Criteria2:=Array(1, s)
0

Ended up doing...

Function GetDatesInPeriodConst(month As Long)
    GetDatesInPeriodConst = 20 + month
    '(January is number 21)
End Function

and

ActiveSheet.Range("$A$29:$CG$3582").AutoFilter field:=18, Criteria1:= _
GetDatesInPeriodConst(month(Date)), Operator:=xlFilterDynamic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.