PDA

View Full Version : Deleting Worksheets Containing Dates Older than X



Incognitus
09-13-2012, 02:32 PM
Hi all,

I've got a macro which looks through all the worksheets in a document and deletes all worksheets that have a name equal to the date ten days before today. And it works!

However, I've since discovered that the worksheets have to contain the date and time - which stops this from working correctly.

Also, in order to find dates older than ten days before today, do I have to make each worksheet name into a variable, truncate this, define it as a date and then do a Greater Than comparison or is there a simpler way?

: pray2:

[I don't have the code available as I'm outside of work, but can make it available if needed]

Simon Lloyd
09-13-2012, 05:06 PM
It would be better to see a sample workbook so we can see your format....etc

Bob Phillips
09-14-2012, 12:38 AM
I've got a macro which looks through all the worksheets in a document and deletes all worksheets that have a name equal to the date ten days before today. And it works!

However, I've since discovered that the worksheets have to contain the date and time - which stops this from working correctly.

Is your code using the Now function rather than the Now function?


Also, in order to find dates older than ten days before today, do I have to make each worksheet name into a variable, truncate this, define it as a date and then do a Greater Than comparison or is there a simpler way?

No it can all be done in a single statement

If DateValue(sh.Name) < Date - 10 Then


[I don't have the code available as I'm outside of work, but can make it available if needed]

That would definitely help.