How to add a named sheet at the end of all Excel sheets? How to add a named sheet at the end of all Excel sheets? vba vba

How to add a named sheet at the end of all Excel sheets?


Try this:

Private Sub CreateSheet()    Dim ws As Worksheet    Set ws = ThisWorkbook.Sheets.Add(After:= _             ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))    ws.Name = "Tempo"End Sub

Or use a With clause to avoid repeatedly calling out your object

Private Sub CreateSheet()    Dim ws As Worksheet    With ThisWorkbook        Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))        ws.Name = "Tempo"    End WithEnd Sub

Above can be further simplified if you don't need to call out on the same worksheet in the rest of the code.

Sub CreateSheet()    With ThisWorkbook        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Temp"    End WithEnd Sub


Kindly use this one liner:

Sheets.Add(After:=Sheets(Sheets.Count)).Name = "new_sheet_name"


ThisWorkbook.Sheets.Add After:=Sheets(Sheets.Count)ActiveSheet.Name = "XYZ"

(when you add a worksheet, anyway it'll be the active sheet)