Copy a worksheet an rename using VBA

lhernandez

Active Member
Joined
May 22, 2006
Messages
282
Hello!
I am currently trying to copy a worksheet and rename the copied worksheet. Based on my other code this could be performed multiple times so there is no way to know the exact name of the worksheet i will be renaming.
I am using the following:

Set ws = Sheets("Sheet1")
ws.Select
ws.Copy after:=Sheets(3)
ws.Name = "Test"

What ends up happening is "Sheet1" is copied after "Sheet3", which at this point would be names "Sheet1 (2)", and the original "Sheet1" is renamed to "Test"...is there a way to set the copied sheet, "Sheet1 (2)" to be renamed as "Test"?

Thanks for your help in advance
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try this.
Code:
Set ws = Sheets("Sheet1")
 
ws.Copy After:=Sheets("Sheet3")
 
Set wsNew = Sheets(Sheets("Sheet3").Index+1)
 
wsNew.Name  = "Test"
 
Upvote 0
Hi, what does
PHP:
 Set wsNew = Sheets(Sheets("Sheet3").Index+1)
mean or do? I follow the rest of the sample but this line I do not understand.

Try this.
Code:
Set ws = Sheets("Sheet1")
 
ws.Copy After:=Sheets("Sheet3")
 
Set wsNew = Sheets(Sheets("Sheet3").Index+1)
 
wsNew.Name  = "Test"
 
Upvote 1
It sets wsNew to refer to the sheet that follows Sheet3, i.e. the sheet that has been copied after Sheet3.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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