issue with autofiltering on a Date criteria

cirugio

Board Regular
Joined
Mar 30, 2010
Messages
130
I am trying to create vba code which will automatically filter on a specific currency = 'GBP' (column 16) and where the maturity date (column 7) is less then today's date. The code does the filtering for the currency but not for the date criteria. Am at a point where I am just spinning my wheels. Hoping someone can assist in determining what I am doing wrong. Thank you for your time in advance.

:confused:
Code:
    Range("A1:Q1").Select
    Selection.AutoFilter
    
    ActiveSheet.Range("$A$1:$Q$1000000").AutoFilter Field:=16, Criteria1:="GBP"
    ActiveSheet.Range("$A$1:$Q$1000000").AutoFilter Field:=7, Criteria1:= _
     "<" & Now(), Operator:=xlAnd
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi,
see if this does what you want:

Code:
With ActiveSheet.Range("A1:Q" & Cells(Rows.Count, "Q").End(xlUp).Row)
    .AutoFilter
    
    .AutoFilter Field:=16, Criteria1:="GBP"
    .AutoFilter Field:=7, Criteria1:="<" & CLng(Date)
    
End With

Dave
 
Upvote 0
This worked beautifully. Thank you Dave :0)
I do have a question though on the CLng(Date) portion of the code. Can you tell me why the date has to be converted to a long integer? Is it because the Date function may have time stamp? Just trying to understand. Thanks again.


Hi,
see if this does what you want:

Code:
With ActiveSheet.Range("A1:Q" & Cells(Rows.Count, "Q").End(xlUp).Row)
    .AutoFilter
    
    .AutoFilter Field:=16, Criteria1:="GBP"
    .AutoFilter Field:=7, Criteria1:="<" & CLng(Date)
    
End With

Dave
 
Upvote 0
This worked beautifully. Thank you Dave :0)
I do have a question though on the CLng(Date) portion of the code. Can you tell me why the date has to be converted to a long integer? Is it because the Date function may have time stamp? Just trying to understand. Thanks again.

Date function returns just the current system Date. Now function returns the Current system Date & Time.

A Date on its own is, or is like a Long Integer - As Excel sees the dates as a US date & way to ensure that Autofilter returns correct result is to just coerce value to a long integer.

When using Autofilter with dates, I normally pass them to a Long variable using DateSerial Function & use this to apply in the filter but sometimes the suggestion given can work ok.

Dave
 
Upvote 0
Thanks for clarifying and making it more efficient. Nice not to have to put the row numbers. Thanks again!!

Date function returns just the current system Date. Now function returns the Current system Date & Time.

A Date on its own is, or is like a Long Integer - As Excel sees the dates as a US date & way to ensure that Autofilter returns correct result is to just coerce value to a long integer.

When using Autofilter with dates, I normally pass them to a Long variable using DateSerial Function & use this to apply in the filter but sometimes the suggestion given can work ok.

Dave
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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