help on Custom filters

compusheaf

Board Regular
Joined
Jun 17, 2004
Messages
195
I am trying to do a custom filter on column A where i have dates in it. I want to do a custom filter as Greater than xyz AND Less Than or equal to abc. The same works when I do it manually in excel, but when i record a code in VBA it does not work. Any reasons ?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi compusheaf

You forgot to post the code, so it's not possible to know what's wrong.

This one works for me:

Columns("A").AutoFilter Field:=1, Criteria1:=">=2007-07-05", Operator:=xlAnd, Criteria2:="<=2007-07-10"

You may have to adapt the date formats to your regional settings.

Kind regards
PGC
 
Upvote 0
Here is the code

Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=">=" & stdate, Operator:=xlAnd _
, Criteria2:="<=" & enddate

stdate and enddate are two string variables storing dates

If I try it doing manually it works but not with this code
 
Upvote 0
What are stdate and enddate? How are they declared and initialized?

I just tried 2 tests, the first one with variables and the second one reading from the worksheet and had no problem:

Code:
Sub Test1()
Dim stdate As Date, enddate As Date

stdate = CDate("2007-07-05")
enddate = CDate("2007-07-10")

Columns("A").AutoFilter Field:=1, Criteria1:=">=" & stdate, Operator:=xlAnd, Criteria2:="<=" & enddate
End Sub

Sub Test2()

Columns("A").AutoFilter Field:=1, Criteria1:=">=" & CDate(Range("d2")), Operator:=xlAnd, _
                                      Criteria2:="<=" & CDate(Range("e2"))
End Sub

Hope this helps
PGC
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,283
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top