Check the error number with Select Case statement
Sub SelectionSqrt()
Dim cell As range
Dim ErrMsg As String
If TypeName(Selection) <> "Range" Then Exit Sub
On Error GoTo ErrorHandler
For Each cell In Selection
cell.value = Sqr(cell.value)
Next cell
Exit Sub
ErrorHandler:
Select Case Err
Case 5 'Negative number
Resume Next
Case 13 'Type mismatch
Resume Next
Case 1004 'Locked cell, protected sheet
MsgBox "The cell is locked. Try again."
Exit Sub
Case Else
ErrMsg = Error(Err.number)
MsgBox "ERROR: " & ErrMsg
Exit Sub
End Select
End Sub
Related examples in the same category