Variables into autofilters

  • Thread starter Thread starter Zippy
  • Start date Start date
Z

Zippy

I know I can input an autofilter such as:

AutoFilterMode = False
.Range("A1:D1").AutoFilter
.Range("A1:D1").AutoFilter Field:=2, Criteria1:=">=35", _
Operator:=xlAnd, Criteria2:="<=45"

But how do I get the two criteria to be read from variables whose values are
input by the user such as a Start_date and an End_date?

TIA.

Zippy.
 
AutoFilterMode = False
.Range("A1:D1").AutoFilter
.Range("A1:D1").AutoFilter Field:=2, _
Criteria1:=">=" & StartDate, _
Operator:=xlAnd, Criteria2:="<=" & EndDate
 
Use an InputBox to prompt the user for the start and end dates, then
simply concantenate them into the criteria expressions (the returned
result from the InputBox is a string), like so (using a UserForm would
entail a lot more work, but you could get both values, do checking on
them together, etc.):

Dim strStartDate As String
Dim strEndDate As String

strStartDate = InputBox("Enter the start date:", "AutoFilter this
List")
strEndDate = InputBox("Enter the end date:", "AutoFilter this List")

.Range("A1:D1").AutoFilter Field:=2, _
Criteria1:=">=" & strStartDate, _
Operator:=xlAnd, _
Criteria2:="<=" & strEndDate

You might want to check the values in the above code before calling the
AutoFilter method (valid dates, etc.). Also, I don't think you need the
following lines in your code, but you can test to be sure:

AutoFilterMode = False
.Range("A1:D1").AutoFilter
 
Back
Top