Variables into autofilters


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.
 
Ad

Advertisements

G

Guest

AutoFilterMode = False
.Range("A1:D1").AutoFilter
.Range("A1:D1").AutoFilter Field:=2, _
Criteria1:=">=" & StartDate, _
Operator:=xlAnd, Criteria2:="<=" & EndDate
 
B

Bill Renaud

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
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top