Problems with putting 2 criteria into an autofiler in vba

cymru23

New Member
Joined
Aug 4, 2009
Messages
1
(Excel 2003, Windows XP Professional)
Hello,
I'm hoping someone can help me with the following problem............... I have written vba code in order to perform an autofilter between two dates, however my macro is not giving any results. If I step through the code, go into excel itself and open up the autofilter I can see that the correct criteria is present, and if i select 'OK' the desired results are given - however I cannot understand why using a macro means that this does not work. Could someone please point me in the right direction, a piece of the code is shown below

Dim dDate As Date
Dim strDate As String
Dim lDate As Long
dDate = DateSerial(YearBegin, MonthFrom, DayFrom)
lDate = dDate
eDate = DateSerial(YearEnd, MonthEnd, DayEnd)
mDate = eDate

If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData

ActiveSheet.UsedRange.AutoFilter Field:=9, Criteria1:=">=" & lDate, Operator:=xlAnd _
, Criteria2:="<=" & mDate


Thanks in advance for any help you can provide

I have also posted this question in the following location, and have had some useful guidance but am still experiencing the same issues:
http://answers.yahoo.com/question/index?qid=20090803084728AAjDB7Z&r=w
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi

Cast both dates to Longs before you process the auitofilter:

Rich (BB code):
ActiveSheet.UsedRange.AutoFilter Field:=9, Criteria1:=">=" & CLng(lDate), Operator:=xlAnd _
, Criteria2:="<=" & CLng(mDate)


Amendments are in red.
 
Upvote 0
I'd be tempted to format them as dates, using FORMAT(). I had a quick mess with some sample data and this seemed to work.
 
Upvote 0

Forum statistics

Threads
1,222,749
Messages
6,167,971
Members
452,158
Latest member
MattyM

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