How to Rename a Sheet in Excel VBA
Renaming a sheet in Excel VBA can be done using the Worksheets("oldName").Name = "newName"
syntax. Let me explain this with an example:
Sub RenameSheetExample()
' Variable to hold the old sheet name
Dim oldName As String
' Variable to hold the new sheet name
Dim newName As String
' Assign the old sheet name
oldName = "Sheet1"
' Assign the new sheet name
newName = "Renamed Sheet"
' Rename the sheet
Worksheets(oldName).Name = newName
End Sub
In the above example, we first define two variables: oldName
and newName
. The oldName
variable represents the current name of the sheet that we want to rename, and the newName
variable represents the new name that we want to assign to the sheet.
Then, using the Worksheets(oldName)
syntax, we access the sheet with the current name and assign a new name using the .Name
property. In this case, the sheet named “Sheet1” will be renamed to “Renamed Sheet”.
This is a simple example to demonstrate the basic concept of renaming a sheet in Excel VBA. You can modify the code as per your requirements, such as using user inputs for the old name and new name, or renaming multiple sheets at once.