I thought this was easy.....autofilter

metropol

Board Regular
Joined
Aug 19, 2005
Messages
209
Scanning this Forum, Ozgrid etc, I can't figure out what I have done wrong....The autofilter with use of a VARIABLE does not work. I have to open the autofilter in column 13 and just press "OK". Then the list appear. What is wrong??

Code:
Sub Datefilter()
   With Sheets("Sales")
          .AutoFilterMode = False
          .Range("A1:M1").AutoFilter
          .Range("A1:M1").AutoFilter Field:=1, Criteria1:="6"   
          .Range("A1:M1").AutoFilter Field:=10, Criteria1:="<100"
          .Range("A1:M1").AutoFilter Field:=13, Criteria1:="<=" & Date <-------Not working!!
   End With
End sub

I have tried to use the function Date. Also tried to assigned Date to a variable uDate instead. Still not working. Declaration of variables etc etc is still NOGO. The Date is correctly displayed in the autofilter dialog.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi Metropol,

As we discussed on this thread:
http://www.mrexcel.com/forum/showthread.php?t=379467

You'll need to convert that date to a long data type.

Untested, but try:
Code:
.Range("A1:M1").AutoFilter Field:=13, Criteria1:="<=" & CLng(Date)

Hope that helps! :)


Colin! Thank you.

Your way worked. I thought I did it like you said in the previous post, but my coding was not sufficient enough. Dont ask way :(
 
Upvote 0
You're welcome. It wasn't so obvious on that other thread because the Date data type --> Long data type conversion was done implicitly by assigning the date value in the cell to a long data type variable. Here, the Date() function returns a Variant (date) which we are passing directly into the autofilter, so we have to explicity convert it to a long.

Hope that makes more sense now.
 
Upvote 0

Forum statistics

Threads
1,214,617
Messages
6,120,541
Members
448,970
Latest member
kennimack

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