Macro for filtering dates

BarryL

Well-known Member
Joined
Jan 20, 2014
Messages
1,436
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
Hi All,

In column F i have a list of dates that change every day (approx. 40 but 20 could be T-1, 10 for T-2, 5 T-3 etc. etc.). Usually the majority are the same so the spread is quite small. What I am looking for is a macro that will add a filter in and leave one date in at a time in the list. As this list changes every day as things update i can't record it. Below is my current code which isnt 100% what i want.

Sub d_filter()
Dim dt As Date
dt = Date$
Z = dt - 1
c = "=" & Z
ActiveSheet.Range("F1").AutoFilter _
field:=6, _
Criteria1:=c

Selection.AutoFilter
Selection.AutoFilter

Z1 = dt - 2
c1 = "=" & Z1
ActiveSheet.Range("F1").AutoFilter _
field:=6, _
Criteria1:=c1
Selection.AutoFilter
Selection.AutoFilter

Z2 = dt - 3
c2 = "=" & Z2
ActiveSheet.Range("F1").AutoFilter _
field:=6, _
Criteria1:=c2

End Sub



any help would be great.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
if there are dates outside of those. i was hoping i wouldnt have to enter that all the way down to like -20 or something.
 
Upvote 0
You want a loop?

Code:
Sub d_filter()
    Dim dt As Date
    Dim i As Long
    Dim c As String
    dt = Date$
    For i = 1 To 20
        With ActiveSheet
            .AutoFilterMode = False
            c = "=" & dt - i
            .Range("F1").AutoFilter _
                field:=6, _
                Criteria1:=c
        End With
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,319
Messages
6,165,307
Members
451,951
Latest member
brentcole

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