Something like below. Of course you need to adapt the number 11 in the code to the one of your error. That's if you want to target a specific error, you can also check if Err.Number <> 0 which is true for all errors.
VBA Code:
Sub Example()
Dim myCalc As Long
' we expect the error after this line
On Error Resume Next
myCalc = 5 / 0 ' raise error
If Err.Number = 11 Then
' Error message
MsgBox "Error: trying to do a division by 0!", vbCritical
' handling the default values to prevent the rest of the code from crashing
myCalc = 1
' resetting the error object
Err.Clear
End If
On Error GoTo 0
Debug.Print myCalc ' Prints 1
End Sub
We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel
Which adblocker are you using?
Disable AdBlock
Follow these easy steps to disable AdBlock
1)Click on the icon in the browser’s toolbar. 2)Click on the icon in the browser’s toolbar. 2)Click on the "Pause on this site" option.
Go back
Disable AdBlock Plus
Follow these easy steps to disable AdBlock Plus
1)Click on the icon in the browser’s toolbar. 2)Click on the toggle to disable it for "mrexcel.com".
Go back
Disable uBlock Origin
Follow these easy steps to disable uBlock Origin
1)Click on the icon in the browser’s toolbar. 2)Click on the "Power" button. 3)Click on the "Refresh" button.
Go back
Disable uBlock
Follow these easy steps to disable uBlock
1)Click on the icon in the browser’s toolbar. 2)Click on the "Power" button. 3)Click on the "Refresh" button.