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:
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

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 :)
 

Watch MrExcel Video

Forum statistics

Threads
1,108,492
Messages
5,523,262
Members
409,506
Latest member
reneekeane

This Week's Hot Topics

Top