Rename worksheet using date entered in cell

steve.waye

Board Regular
Joined
Jul 8, 2010
Messages
68
Hi all,

I'm after a way to automatically rename a worksheet based on a date entered in cell D2.


The date has slashes (/) so I'm aware these can't be used in the name of the worksheet. I assume they can be changed in the code.


It would be ideal if a date of 28/10/18 in cell D2 became 28-Oct-18 as the sheet name.


Also, I want to be able to click a button on the active worksheet to create a copy (new worksheet) and have a popup that allows you to enter the date for cell D2 on the new worksheet, which will subsequently rename the new worksheet.


Hope this makes sense.


It's been a while since I did any VBA so guidance on where to paste the code would also be appreciated.


Thanks.
 
I think I have it...

Code:
Sub NewWorksheet()
    Dim DateD2, NewWks As Worksheet
    DateD2 = ActiveSheet.Range("D2").Value
    ActiveSheet.Copy after:=ActiveSheet
    Set NewWks = ActiveSheet
    ActiveSheet.Range("C9:E15").Clear
    ActiveSheet.Range("G9:G15").Clear
    ActiveSheet.Unprotect
    NewWks.Range("D2").Value = DateD2 + 7
    NewWks.Range("D2").Cells.Locked = True
    ActiveSheet.Protect
    ActiveSheet.Name = Format(Range("D2"), "dd-mmm-yy")
End Sub

Thanks for all your help Minuteman. It has encouraged me to get back into VBA (it's been a while).

Cheers.

It is Momentan :)

Range.Clear would clear both contents nd formatting, while Range.ClearContents would clear only the content
 
Last edited:
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
It is Momentan :)

Range.Clear would clear both contents nd formatting, while Range.ClearContents would clear only the content

Thanks Momentan (or is it Momentman as per your username)?


That last bit of code helped immensely.


Using "Range.Clear" resulted in having to write additional code to reformat all of the associated cells.


"Range.ClearContents" is so much better.


Thanks again. :-)
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,896
Members
452,948
Latest member
Dupuhini

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