I want to copy a sheet and add it to the end of all current sheets (regardless of whether the sheets are hidden).
Sheets(1).Copy After:=Sheets(Sheets.Count) Sheets(Sheets.Count).name = "copied sheet!"
This works fine, except, when there are hidden sheets, the new sheet is only inserted after the last visible worksheet, so the name
command renames the wrong sheet.
I have tried variations of the following to get a reference to the newly copied WorkSheet
but none were successful and/or valid code.
Dim test As Worksheet Set test = Sheets(1).Copy(After:=Sheets(Sheets.Count)) test.Name = "copied sheet!"
VBA code: move active sheet to front of current workbookAfter pasting the code, press the F5 key to run the code. Then the active sheet will be moved to end or front of current workbook immediately.
On the Edit menu, click Sheet > Move or Copy Sheet. On the To book menu, click the workbook that you want to copy the sheet to. Tip: To create a new workbook that contains the moved sheet, click new book. In the Before sheet box, click the sheet that you want to insert the copied sheet before, or click move to end.
Shift to the worksheet you want to copy to end of workbook, right click the sheet tab, and then click Move or Copy from the context menu. See screenshot: 2. In the Move or Copy dialog box, select (move to end) in the Before sheet box, and check the Create a copy box, then click the OK button.
Try this
Sub Sample() Dim test As Worksheet Sheets(1).Copy After:=Sheets(Sheets.Count) Set test = ActiveSheet test.Name = "copied sheet!" End Sub
Looking back at this, a better approach would be
Set test = Sheets(Sheets.Count)
As correctly mentioned in the comments below, there are lot of things that needs to be considered when copying and renaming a sheet. Would recommend checking the other answers as well.
Make the source sheet visible before copying. Then copy the sheet so that the copy also stays visible. The copy will then be the active sheet. If you want, hide the source sheet again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With