asked on
I want to copy a WorkSheet w/VBA and specify the name
I want to copy a workSheet w/VBA.
I can use the following:
Sheets("Sheet1").copy Before:=Sheets(1)
ActiveSheet.Name = "Sheet1.bak"
But this method relies on the copied sheet always becoming the 'ActiveSheet'.
This seems like it could be prone to renaming the wrong sheet ?!
Is there no way to supply and/or force a name into copy command ?
Thanks
Is there no way to supply and/or force a name into copy command ?
No there isn't. What you have is correct. The copied sheet will be activated (unfortunately the Copy method doesn't return a reference to the worksheet). You could alternatively add a new worksheet (which does return a reference), rename it, then copy the contents of the source sheet.
Seems like overcomplicating life for a problem you don't actually have. Also, sheet names can include commas so using a comma delimited list of sheet names is still prone to error.
ASKER
Thanks for the heads up on the commas.
So you are saying you have actually encountered the wrong sheet being renamed, rather than your original statement that "This seems like it could be prone to renaming the wrong sheet "?
ASKER
Curious. I have never seen it happen. Anyway, given that you know where you are copying it to, you could simply refer to it by position.
Try this:
Open in new window