QP School
Create a new worksheet and rename it with VBA - Printable Version

+- QP School (https://qomplainerzschool.lima-city.de)
+-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3)
+--- Forum: Excel VBA Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=48)
+--- Thread: Create a new worksheet and rename it with VBA (/showthread.php?tid=5244)



Create a new worksheet and rename it with VBA - Qomplainerz - 07-27-2023

Example:

Sub CreateAndRenameSheet()
    Dim newSheet As Worksheet
   
    'Add a new worksheet and store the reference to the new sheet in the variable "newSheet"
    Set newSheet = ThisWorkbook.Sheets.Add
   
    'Rename the new worksheet to "NewSheet"
    newSheet.Name = "NewSheet"
End Sub

Explanation:

The code defines a subroutine named CreateAndRenameSheet.
It declares a variable newSheet to store the reference to the newly created worksheet.
The Sheets.Add method is used to add a new worksheet to the workbook, and the reference to the new sheet is assigned to the newSheet variable.
The Name property of the newSheet variable is set to "NewSheet" using the assignment statement (newSheet.Name = "NewSheet"), effectively renaming the newly created worksheet.