Rename Copied Tab Based On Ascending Weekly Sequence

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
210
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a macro that copies the current sheet and then clears out old data so I can update it, but I can't figure out how code it to rename the tab using the weekly sequence that I use (the naming convention is Wk 1, Wk 2, Wk 3, etc.). So when Wk 33 is copied, I would manually rename it to Wk 34, but I don't know how to code that. Can anyone help?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,

to rename your sheet, you could use something like this: ?

Rgds
Rob

VBA Code:
Sub renamesheet()


Dim Ws As Worksheet
 
    this_week = Application.WorksheetFunction.WeekNum(Now()) - 1 'saves it as last weeks week no.
    
    Set Ws = Worksheets("Sheet1")

    Ws.Name = "wk" & this_week


End Sub
 
Upvote 0
Run-time error '9':
Subscript out of range on this part:

VBA Code:
Set Ws = Worksheets("Sheet1")
 
Upvote 0
maybe you don't have a "sheet1" in your workbook ?

this would normally be the sheet you want to rename to something else ?
 
Upvote 0
No, all the tabs are named for the various weeks of the year. When I copy the current sheet, it's renamed to the previous week (2), so Wk 33 (2) in this case.
 
Upvote 0
so whats the name of the tab that you want to rename as week 34 ?
 
Upvote 0
ok, thanks, sorry, I missed that point. So try this instead: It works based on the current sheet you work on.

VBA Code:
Sub renamesheet()


Dim Ws As Worksheet
 
    this_week = Application.WorksheetFunction.WeekNum(Now())
    
    Set Ws = ActiveSheet 'Worksheets("Sheet1")

    Ws.Name = "wk " & this_week


End Sub
 
Upvote 0
Solution
I actually worked out the logic on my own using your initial code as a launching point. This is what I came up with and just tested it. Works fine and in the format I need:


VBA Code:
ActiveSheet.Name = "New Week"
    Dim Ws As Worksheet
 
    this_week = Application.WorksheetFunction.WeekNum(Now()) - 1 'saves it as last weeks week no.
    
    Set Ws = Worksheets("New Week")

    Ws.Name = "Wk " & this_week

I had to tinker a little to get Wk 34 instead of wk34, which is what your code did. Thanks, you did the hard work. :)
 
Upvote 0
yep, I realised I hadnt added the space, so corrected it with my last post hoping you hadn't noticed :)

thanks for feedback - glad to have been of use..

Rob
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,559
Members
448,970
Latest member
kennimack

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