First I selected "Yes" to the question "Change Worksheet Name?". Then the message "Type new Worksheet Name" appears. Instead of typing in a new name and selecting "OK", I select the "cancel" button and my error messages are displayed. How do I avoid seeing the error messages and just let the macro end "quietly"?
Option Explicit ' Force explicit variable declaration.
Sub ChangeSheetName()
Dim Carryon As String
On Error GoTo eh
Carryon = MsgBox("Change Worksheet Name?", vbYesNo)
If Carryon = vbYes Then
Dim shName As String
Dim currentName As String
currentName = ActiveSheet.Name
shName = InputBox("Type new Worksheet name")
ThisWorkbook.Sheets(currentName).Name = shName
End If
Exit Sub
eh:
MsgBox "The following error occured." _
& vbCrLf & "" _
& vbCrLf & "Error Number is: " & Err.Number _
& vbCrLf & "" _
& vbCrLf & "Error Description is: " & Err.Description _
& vbCrLf & "" _
& vbCrLf & "You likely hit the Esc key to stop renaming the Worksheet." _
& vbCrLf & "" _
& vbCrLf & "No worries. You can try again to rename or leave it as is." _
& vbCrLf & "" _
& vbCrLf & "No harm done."
End Sub
You've declared Carryon as a string variable - vbYes (and other messagebox results) are numeric constants. Change
Dim Carryon As StringtoDim Carryon As Long