vba to filter dates

don matteo

Board Regular
Joined
Nov 14, 2016
Messages
51
Hey guys
I have 100s of rows in column "I" with the date format
2017-04-24 14:10:23

<tbody>
</tbody><colgroup><col></colgroup>


I want to create a macro to filter and hide anything that is 48 hours old, and only show me the items older then 48 hours.

I have tried to record a macro by my filtering it but I cant get that to work because theres 100s of rows and the dates are never the same due to the time stamp

any help will be appreciated
 
hey guys thanks,
so this is a screen shot of my data

8cnlYEs.jpg

and this is how it looks after I run the macro
TyvOqJM.jpg
 
Last edited:
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I can't see the screenshots for some reason. They're just little x's (probably because I'm on my work computer). And MrIfOnly seems to have had a solution that he was able to get to work for him. I just thought I'd share this simple little code that seems to do what you're requesting. You'll have to make some adjustments to make it fit your application.

Code:
Dim lRow As Long
Dim rng1 As Range
lRow = Sheet1.Range("I" & Rows.Count).End(xlUp).Row
Set rng1 = Sheet1.Range("I1:I" & lRow)
For Each Cell In rng1
    If CDate(Cell.Value) <= Now - 2 Then
        Sheet1.Range("A" & Cell.Row).EntireRow.Hidden = True
    End If
Next Cell
 
Upvote 0
thank you I will try it when I get to the office!

I can't see the screenshots for some reason. They're just little x's (probably because I'm on my work computer). And MrIfOnly seems to have had a solution that he was able to get to work for him. I just thought I'd share this simple little code that seems to do what you're requesting. You'll have to make some adjustments to make it fit your application.

Code:
Dim lRow As Long
Dim rng1 As Range
lRow = Sheet1.Range("I" & Rows.Count).End(xlUp).Row
Set rng1 = Sheet1.Range("I1:I" & lRow)
For Each Cell In rng1
    If CDate(Cell.Value) <= Now - 2 Then
        Sheet1.Range("A" & Cell.Row).EntireRow.Hidden = True
    End If
Next Cell
 
Upvote 0
Hopefully Peter's solution will work for you.

As for mine: I tried various formatting styles and can't seem to duplicate your issue. After running my code, when I hover over the filter icon in column I it shows "Before: " and the date minus 48 hours not the "Less than" and the date code that is shown in your screenshot. What version of Excel are you running?
 
Upvote 0
I am running 2013, what I ended up doing was using a MID formula to remove the timestamp from the date and filtered it out that way, im assuming the format of my data was just giving me some issues

thank you all for all the help! I really needed it!!![

QUOTE=MrIfOnly;4835460]Hopefully Peter's solution will work for you.

As for mine: I tried various formatting styles and can't seem to duplicate your issue. After running my code, when I hover over the filter icon in column I it shows "Before: " and the date minus 48 hours not the "Less than" and the date code that is shown in your screenshot. What version of Excel are you running?[/QUOTE]
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,553
Members
449,038
Latest member
Guest1337

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