Macro to select uk format date range within sheet of date

edmundmckay

New Member
Joined
Aug 24, 2015
Messages
31
Hi

I currently have the below macro which I am trying to achieve. The aim is using a control set of two dates as a range on a tab called "Macro Button" and applying them to select a range of dates in column C on a sheet called "Rent Arrears".

The below macro works in the scenario if I just wanted to filter on two specific ranges but the actually custom filter is to filter first anything prior to Daterange1 (C3) on Macro button tab and then filter anything after Daterange2 (C4) on Macro Button tab.

I have tried to insert "<" & in front of criteria1 and 2 in the code but all this does is filter column C with no results.

Please could some point me with the right amendment to achieve the filter range desired.

Sub dateboxfiltermacro()


Dim Lastrow As Long
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Dim Daterange1 As String
Dim Daterange2 As String
Daterange1 = Sheets("Macro Button").Range("C3").Value
Daterange2 = Sheets("Macro Button").Range("C4").Value
ActiveSheet.Range("A5:N5" & Lastrow).AutoFilter Field:=3, Criteria1:=Daterange1, Operator:=xlOr, Criteria2:=Daterange2

End Sub

many thanks in advance
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi,

Most probably you are working with actual dates ... or more precisely numbers ...

As a consequence instead of

Code:
[COLOR=#333333][I]Dim Daterange1 As String[/I][/COLOR]
[COLOR=#333333][I]Dim Daterange2 As String[/I][/COLOR]

you should test
Code:
[COLOR=#333333][I]Dim Daterange1 As Value[/I][/COLOR]
[COLOR=#333333][I]Dim Daterange2 As Value[/I][/COLOR]

Hope this will help
 
Upvote 0
you should test
Code:
[COLOR=#333333][COLOR=#333333][I]Dim Daterange1 As Value[/I][/COLOR]
[COLOR=#333333][I]Dim Daterange2 As Value[/I][/COLOR][/COLOR]
"Value" is not a valid data type, and will return compile errors.
 
Upvote 0
Oops ... I meant Date ... and Not Value ...
 
Upvote 0
Hi James and Joe.

Thanks for getting involved so quickly with the query. So I have changed the variant to Date as below but sadly still doing the same as before. I have inserted a screen shot of what result is happening on the data. If you actually manually go into the custom filter, you can read that the macro filter instructions have been met but for some reason those items in column C aren't being selected.
Any thoughts on this?

Dim Daterange1 As Date
Dim Daterange2 As Date


Mr_Excelquerysnipfiltercodeissue.jpg


cheers
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,597
Members
449,038
Latest member
Arbind kumar

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