Delete Rows Over 3 Months Old as of Today (VBA)

C45P4R

New Member
Joined
Oct 15, 2014
Messages
39
Hi all. I decided to start making macros at work for a few of our processes. I want to learn more about Excel. At the start of this project I had no VBA knowledge what-so-ever. I now have almost none :LOL:. So far I've based a lot of the code from another macro and with the rest from Google searches and trial and error. Anyway, I'm stuck!

I need to delete all data which is over 2 months old from current date. I found a suggestion from another thread, but can't get it to work. To be honest, it's hard to work with because I don't really know what a lot of it means...

Sub
With Sheets("Booked")
LR = .Cells(Rows.Count, "AE").End(xlUp).Row
For i = LR To 2 Step -1
If .Cells(i, "AE").Value < DateAdd("m", -2, Date) Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
End Sub

It seems to just cycle through the stages again and again without actually deleting anything.

I also need to label the data in a separate column based on the date relative to the day the macro is run. I can't find how to do this either. Any help would be much appreciated.
 
Also, in relation to the comment by Neon on part of his code:

Application.EnableEvents = False ' Turn calcs to manual so excel stop calculating everything on change

I have a sheet with lots of formula that pulls data from other sheets etc. Should I just be turning calculations off for the entire macro process and switch them back on right at the end? Would it make much of a difference to the time it takes the macro to run its course? Every time a sheet is changed, it will cause changes in the first sheet.

I can't say how much time it will save. And remember, usually we're talking about shaving off seconds from runtime. Part of it is just a big to do about nothing. If you have a lot of 'heavy' formulas such as sumproduct, vlookup, or other array formulas, waiting to recalc until after will save time. If you want to test it out, try to put 10,000 formulas in a sheet. Make them simple, eg :=A1+1 and copy that all over the place. Then, if you hit F9, it might take a few seconds.

It's really just trying to squeeze out as much redundancy and chip speed as we can. As for resources, here is a great one. This forum is a nice place to learn. Really, what I will say, is be specific in your google searches. I always search for the function I want to know more about. Example:

VBA instr

Searching that will come up with the instr page and that will detail what you need for that function. Don't be afraid to post a bunch of questions, that's why we're here after all.
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,215,884
Messages
6,127,567
Members
449,385
Latest member
KMGLarson

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