To rename the active sheet in Excel VBA, you can use the ActiveSheet.Name
property to get the current name of the active sheet and assign a new name to it. Here’s an example:
Sub RenameActiveSheet()
Dim newSheetName As String
newSheetName = InputBox("Enter the new sheet name:") ' Prompt the user to enter a new name
If newSheetName = "" Then ' Check if the user provided a name
MsgBox "Please enter a name to rename the active sheet."
Exit Sub
End If
' Rename the active sheet
ActiveSheet.Name = newSheetName
' Optional message to show the new sheet name
MsgBox "The active sheet has been renamed to '" & newSheetName & "'."
End Sub
In this example, the code prompts the user to enter a new sheet name using the InputBox
function. If the user provides a name, the code renames the active sheet using the ActiveSheet.Name
property. Finally, a message box is displayed to show the new sheet name. If the user does not provide a name, a message box is displayed to remind them to enter a name.