Macro to show dates between today's date and 6 weeks in the past

Twistatron

New Member
Joined
Sep 24, 2014
Messages
5
Hello, I'm new on these forums and I was wondering if anyone could help me with creating a macro.

What I need to do is filter a database to show only rows that contain a date between today's date and 6 weeks in the past. If this isn't possible, perhaps a popup box where I can enter two values such as today's date and the date I would like to go back to and the macro could show me every entry in between those values?

Apologies if this has been asked before or seems too simple, I'm very new at creating macros!

Thanks in advance for anyone who can help me with this :biggrin:
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Assuming your dates are in column "A", then if you place this code in a Commandbutton, then First click will hide the rows , second click will unhide them.
Code:
[COLOR="Navy"]Sub[/COLOR] MG24Sep22
[COLOR="Navy"]Dim[/COLOR] Rng         [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn          [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dt          [COLOR="Navy"]As[/COLOR] Date
[COLOR="Navy"]Dim[/COLOR] nRng        [COLOR="Navy"]As[/COLOR] Range
Dt = DateAdd("d", -42, Date)


[COLOR="Navy"]If[/COLOR] ActiveSheet.Rows.Hidden [COLOR="Navy"]Then[/COLOR]
    ActiveSheet.Rows.Hidden = False
[COLOR="Navy"]Else[/COLOR]
    [COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        [COLOR="Navy"]If[/COLOR] Dn < Dt Or Dn > Date [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]Set[/COLOR] nRng = Dn
            [COLOR="Navy"]Else[/COLOR]
                [COLOR="Navy"]Set[/COLOR] nRng = Union(nRng, Dn)
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Dn
nRng.EntireRow.Hidden = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 

Twistatron

New Member
Joined
Sep 24, 2014
Messages
5
Thanks Mick! That's perfect!

Also I didn't know Command buttons were a thing either, so you've helped me twice here :)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,701
Messages
5,833,215
Members
430,197
Latest member
edeibold

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
Top