Excel date vba filter

Tommy5

New Member
Joined
Jun 2, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

Been stuck on this for days now so I'm resorting to asking for assistance on this one.

I'm trying to filter between two dates in dd/mm/yyyy format ( my PC is formatted this way too in regional settings), but after I run the code, the date "custom filter" has mixed up the days and months.

Here is my code, where c3 is the start date, formatted dd/mm/yyyy, and c4 is end date, formatted dd/mm/yyyy, Sheet10.Range("a4") is the table of data, and column 34 is the column I need to apply the date filter to.

Dim DateBegin As Date
Dim DateEnd As Date
Dim Rng As Range

Set Rng = Sheet10.Range("a4")
DateBegin = Format(Sheet1.Range("c3").Value, "dd/mm/yyyy")
DateEnd = Format(Sheet1.Range("c4").Value, "dd/mm/yyyy")
With Rng
.AutoFilter Field:=34, Criteria1:=">" & DateBegin, _
Operator:=xlAnd, Criteria2:="<=" & DateEnd

What am I doing wrong here? Would be so grateful if somebody could help me out on this one,
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi,
welcome to forum

dates in AutoFilter you need to think US date format – one method used to overcome this is to coerce your date to long data type using type conversion function Clng.

VBA Code:
Dim DateBegin As Long
Dim DateEnd As Long
Dim Rng As Range

Set Rng = Sheet10.Range("a4")
DateBegin = CLng(Sheet1.Range("c3").Value)
DateEnd = CLng(Sheet1.Range("c4").Value)

Rng.AutoFilter Field:=34, Criteria1:=">" & DateBegin, _
                        Operator:=xlAnd, _
                        Criteria2:="<=" & DateEnd

Note: variables have been changed from Date to Long data type.

If still have issues, then perhaps using the DateSerial function to pass to Long variables will resolve

Dave
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
With Sheet10
   .Range("A4").AutoFilter 16, ">" & Sheet1.Range("C3").Value2, _
      xlAnd, "<=" & Sheet1.Range("C4").Value2
End With
 
Upvote 0
Hi,
welcome to forum

dates in AutoFilter you need to think US date format – one method used to overcome this is to coerce your date to long data type using type conversion function Clng.

VBA Code:
Dim DateBegin As Long
Dim DateEnd As Long
Dim Rng As Range

Set Rng = Sheet10.Range("a4")
DateBegin = CLng(Sheet1.Range("c3").Value)
DateEnd = CLng(Sheet1.Range("c4").Value)

Rng.AutoFilter Field:=34, Criteria1:=">" & DateBegin, _
                        Operator:=xlAnd, _
                        Criteria2:="<=" & DateEnd

Note: variables have been changed from Date to Long data type.

If still have issues, then perhaps using the DateSerial function to pass to Long variables will resolve

Dave


Thank you so much, that worked!

Could you provide an example for using the DateSerial function to pass to Long variables
 
Upvote 0
Thank you so much, that worked!

Could you provide an example for using the DateSerial function to pass to Long variables

VBA Code:
With Sheet1.Range("c3")
    DateBegin = DateSerial(Year(.Value), Month(.Value), Day(.Value))
End With

With Sheet1.Range("c4")
    DateEnd = DateSerial(Year(.Value), Month(.Value), Day(.Value))
End With

In most cases, clng coercion normally works with dates for Autofilter

many thanks for feedback, glad we were able to assist you

Dave
 
Upvote 0

Forum statistics

Threads
1,223,532
Messages
6,172,878
Members
452,486
Latest member
standw01

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