VBA line of programming fails

I have incorporated the following criteria into a userform but it will not compile and fails with the following error message

This is a if then else construct and I have used a similar format several times in the coding but this one keeps erroring

CODING

.Range("A1").CurrentRegion.AutoFilter Field:=8, Criteria1:>=Me.txtstartdate.Value, Operator:=xlAnd, criteria2:<=Me.txtenddate.Value

Highlights Criteria1 and then errors out with "Compile error expected named parameter"

.Range("A1").CurrentRegion.AutoFilter Field:=8, _
    Criteria1:=">="&format(cdate(Me.txtstartdate.Value),"mm/dd/yyyy"), _
    Operator:=xlAnd, _
    criteria2:="<="&format(cdate(me.txtenddate.Value),"mm/dd/yyyy")

When you're using the named parameters, the := is the symbol for assigning a
value to those paramaters.   You can't change this.

And it looks like your values in the userform(?) are representing dates.  They
can cause trouble in autofilter.

Saved from a previous post:

This is from "Excel 2002 VBA Programmer's Reference"
Written by John Green, Stephen Bullen, Rob Bovey and Robert Rosenberg

http://www.oaltd.co.uk:80/ExcelProgRef/Ch22/ProgRefCh22.htm
Search for "Range.AutoFilter" and you'll see this note:

Range.AutoFilter

The AutoFilter method of a Range object is a very curious beast. We are forced
to pass it strings for its filter criteria and hence must be aware of its string
handling behaviour. The criteria string consists of an operator (=, >, <, >=
etc.) followed by a value.

If no operator is specified, the "=" operator is assumed. The key issue is that
when using the "=" operator, AutoFilter performs a textual match, while using
any other operator results in a match by value. This gives us problems when
trying to locate exact matches for dates and numbers.

If we use "=", Excel matches on the text that is displayed in the cell, i.e. the
formatted number. As the text displayed in a cell will change with different
regional settings and Windows language version, it is impossible for us to
create a criteria string that will locate an exact match in all locales.

There is a workaround for this problem. When using any of the other filter
criteria, Excel plays by the rules and interprets the criteria string according
to US formats. Hence, a search criterion of ">=02/01/2001" will find all dates
on or after 1st Feb, 2001, in all locales.

We can use this to match an exact date by using two AutoFilter criteria. The
following code will give an exact match on 1st Feb, 2001 and will work in any
locale:

   Range("A1:D200").AutoFilter 2, ">=02/01/2001", xlAnd, "<=02/01/2001"

BJ_thebear wrote:


I have incorporated the following criteria into a userform but it will not compile and fails with the following error message

This is a if then else construct and I have used a similar format several times in the coding but this one keeps erroring

CODING

.Range("A1").CurrentRegion.AutoFilter Field:=8, Criteria1:>=Me.txtstartdate.Value, Operator:=xlAnd, criteria2:<=Me.txtenddate.Value

Highlights Criteria1 and then errors out with "Compile error expected named parameter"

--

Dave Peterson

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated March 26, 2024 Views 2,775 Applies to: