error 91

todros

New Member
Joined
Feb 18, 2016
Messages
8
Below is the macro I use
I search names and if they match I color them.

Cells.FindNext(After:=ActiveCell).Activate
Cells.Find(What:="mount zion", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With

If the name in "WHAT" is not in the file the macro stops and generates "error 91". How can i avoid the macro from stopping and continue to the next routine?
 
I think this is basic code to avoid Error91 for find method.(not findnext method)

Code:
Sub test()
    Dim FoundCell As Range
    Set FoundCell = Range("A1:A10").Find(What:="mount zion")
    If FoundCell Is Nothing Then
        MsgBox "Not Found"
    Else
        With FoundCell.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
End Sub

However, this find statement might confuse users because options are linked with users operation.
I use this Array type code. This code is faster than find method.

Code:
Sub find()
    Dim i As Long, j As Long
    Dim rng As Range
    Dim x
        Set rng = Range("A1:D15")
        x = rng
        For i = LBound(x) To UBound(x)
            For j = 1 To UBound(x, 2)
            If x(i, j) Like "*mount zion*" Then
                With rng.Cells(i, j).Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .Color = 255
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
            End If
            Next
        Next
 End Sub

Hope this helps.
 
Last edited:
Upvote 0
bro add a error handler

on error goto errhandler
Cells.FindNext(After:=ActiveCell).Activate
Cells.Find(What:="mount zion", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
exit sub
Errhandler:
call Macro2

end sub




 
Upvote 0

We've detected that you are using an adblocker.

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.
Go back
Back
Top