Rukgo
Rukgo

Reputation: 121

Excel VBA Change name of copied active sheet

Below is the code that I have. I want to apply this to any excel file and create a copy of any sheet selected and rename its copy to Updated.

    ActiveWorkbook.ActiveSheet.Select
    ActiveWorkbook.Activesheet.copy After:=Sheets(1)
    *Sheets("page (2)").Select
    Sheets("page (2)").Name = "Updated"*

How do I select the new copied worksheet. The section surrounded by * is the issue, I just don't know what to change.

Upvotes: 2

Views: 1606

Answers (1)

brettdj
brettdj

Reputation: 55682

You should be checking if the Updated name already exists.

Sub UpdatenMove()
Dim ws As Worksheet

On Error Resume Next
Set ws = Sheets("Updated")
On Error GoTo 0

If Not ws Is Nothing Then
    MsgBox "This sheet already exists", vbCritical
Else
ActiveSheet.Copy After:=Sheets(1)
ActiveSheet.Name = "Updated"
End If

End Sub

Upvotes: 2

Related Questions