I need to be able to use variables as my criteria for an autofilter which I run through VBA. I know how to use the xlAnd operator to create a custom autofilter based on 2 static values e.g.:
Selection.AutoFilter Field:=1, Criteria1:=">=5", Operator:=xlAnd, 'Criteria2:="<=10"
Gives me everything in Col.1 from 5 - 10 inclusive.
However, I want to be able to filter based on two dates which the user will have entered earlier in the program.
I want to do something like:
Selection.AutoFilter Field:=4, Criteria1:=>=StartDate, Operator:=xlAnd, Criteria2:=<=EndDate
(where StartDate and EndDate are the variables)
I have also tried using the xlBetween operator to get around the problem but don't think that's right either!:
Selection.AutoFilter Field:=3, Criteria1:=StartDate, Operator:=xlBetween, Criteria2:=EndDate
And even tried this (I was getting pretty desperate!), but I don't think it was ever going to work!
Selection.AutoFilter Field:=4, Criteria1:=StartDate, Operator:=xlGreaterEqual, Operator:=xlAnd Criteria2:=EndDate, Operator:=xlLessEqual
Selection.AutoFilter Field:=1, Criteria1:=">=5", Operator:=xlAnd, 'Criteria2:="<=10"
Gives me everything in Col.1 from 5 - 10 inclusive.
However, I want to be able to filter based on two dates which the user will have entered earlier in the program.
I want to do something like:
Selection.AutoFilter Field:=4, Criteria1:=>=StartDate, Operator:=xlAnd, Criteria2:=<=EndDate
(where StartDate and EndDate are the variables)
I have also tried using the xlBetween operator to get around the problem but don't think that's right either!:
Selection.AutoFilter Field:=3, Criteria1:=StartDate, Operator:=xlBetween, Criteria2:=EndDate
And even tried this (I was getting pretty desperate!), but I don't think it was ever going to work!
Selection.AutoFilter Field:=4, Criteria1:=StartDate, Operator:=xlGreaterEqual, Operator:=xlAnd Criteria2:=EndDate, Operator:=xlLessEqual