QP School

Full Version: Create a new worksheet and rename it with VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.