VBA Code to find a date and delete worksheet with a matching name?

Tarver

Board Regular
Joined
Nov 15, 2012
Messages
109
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Could you help with VBA code to find and delete the right Worksheet?

I have a report that requires that five days' worth of data be saved on five separate tabs, one tab per day. Those tabs are automatically created and named by my VBA code. However, I am having trouble identifying the sheet that's six days old and deleting it.

This code creates and renames my sheet to the current date.

VBA Code:
Sheets.Add After:=ActiveSheet
NameToday = Format(Date, "MM-DD-YYYY"
ActiveSheet.Name - NameToday

However, my problem is that I am not able to write the code to:
1 - count back six days to identify the correct date (NameToday-6 hasn't worked, but I've tried to do a simple formula in the sheet itself and then read that value with my code.)
2 - format the Worksheet.Name to be in the correct format.
3 - Delete the sheet with the matching name.

I'd appreciate any suggestions you can offer! Clearly I'm out of my element when writing code, but I'm trying to learn.

Thanks!
 

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)
1 - count back six days to identify the correct date (NameToday-6 hasn't worked, but I've tried to do a simple formula in the sheet itself and then read that value with my code.)
That is because the FORMAT function returns a String, not a date, so you cannot perform mathematical computations on it.
Try this instead:
Rich (BB code):
DeleteName = Format(Date - 6, "MM-DD-YYYY")
 
Upvote 0
Solution
Thanks, Joe4! That was enough to get me over the hurdle! Thank you.
 
Upvote 0
You are welcome!
Glad I was able to help.
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,947
Members
449,198
Latest member
MhammadishaqKhan

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