I'm creating a template that will contain macros intended for people who don't know anything about VBA and once I'm gone, there will be no one around to debug issues for them, so I'm trying to build in some error handling to help them out if they ever encounter an issue.
In this piece of code, I'm creating a sheet called ConfirmedPivot. At first, I included a simple error handling GoTo 0 which says "This name already exists" but I'm afraid this instruction is not clear enough for users as to how to fix the error.
So I created ConfPivError message which gives better instructions on what to do but I can't figure out what is the best button to go with this message. I want a button that will stop the code so they can delete the duplicate name and start over (equivalent of End button). I tried few buttons but none of them seem to be able to stop the code. All of the OK, Abort and Cancel buttons don't stop the code (so the code tries to run again and the error comes up again).
Do you have any ideas of which button I could use? Or alternatively, could I add something to end the code if this error comes up once the user presses OK?
' Create ConfirmedPivot
Dim wsTest As Worksheet
Const strSheetName As String = "ConfirmedPivot"
Set wsTest = Nothing
On Error Resume Next
Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
On Error GoTo ConfPivError ' custom error message
If wsTest Is Nothing Then
Worksheets.Add.Name = "ConfirmedPivot"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Recovered_Sheet1!R1C1:R65536C114", Version:=xlPivotTableVersion10). _
CreatePivotTable TableDestination:="'ConfirmedPivot'!R1C1", TableName:="PivotTable4" _
, DefaultVersion:=xlPivotTableVersion10
End If
' end Create ConfirmedPivot
' Error handling experiment
ConfPivError:
Answer = MsgBox("ConfirmedPivot Sheet already exists in this workbook. If you don't need it, delete it then try the button again" _
& "(New ConfirmedPivot will be created). If you still need it, rename it and try the button again.", vbOKOnly, "ConfirmedPivot already exists!")
' end experiment