SM123
SM123

Reputation: 59

VBA Detecting an error and stopping the code

I currently have this, but I dont necessarily need a loop. I just need to see if there is any error in the usedrange, and if yes, stop the code and display the message. If no error, then just continue with the rest of the steps that are after this part of the code. Right now this loop displays this message for every error detected. I just need it to be displayed once if any error is found, and then stop the code.

Dim r As Range

For Each r In ws_DST.UsedRange
    If IsError(r.Value) Then
        MsgBox "Please fix any errors", vbOKOnly
    End If
Next
End 

Upvotes: 0

Views: 41

Answers (1)

Gary's Student
Gary's Student

Reputation: 96791

How about:

Sub errortest()
    On Error Resume Next
    Set Rng = Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
    On Error GoTo 0
    If Rng Is Nothing Then
        Exit Sub
    Else
        For Each r In Rng
            MsgBox r.Address(0, 0)
        Next r
    End If
End Sub

Upvotes: 2

Related Questions