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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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