"On Error" Statement nested in for loop

merin95

New Member
Joined
Jun 16, 2015
Messages
2
Hello,

I'm new to VBA Programming, but my first program may be a little more complicated than I can handle. Here is a segment from my code:
Code:
On Error GoTo Error_Handler
For i = 2 To top
    With Range("I" & i + 57)
        .Formula = "=VLOOKUP(H" & i + 57 & ",F$59:G$" & top + 57 & ",2, FALSE)"
        .Font.Name = "Arial"
        .Font.Size = 8
    End With
    
    If Range("I" & i + 57) = "MATCH" Then
        Range("H" & i + 57).Interior.ColorIndex = 7
        GoTo Loop_Resume
    End If
Loop_Resume:
Next i

Error_Handler:
    Err.Clear
    On Error GoTo Error_Handler
    GoTo Loop_Resume

The VLOOKUP function will determine if the value in a cell matches a cell in a different data collection. If it is a match, the cell next to it will have the value "MATCH", but if it doesn't, the result comes out to be "#N/A". I want to be able to highlight the matched cells in pink, and I want to go about doing this with an error handler. So far, I've got the loop going through the "MATCH"'s and the first error just fine, but as soon as it gets to the second error, it gives me the runtime error "type mismatch".

I've tried several variations of this code and this is the most luck I've had. Thanks in advance to anyone who offers a solution!
 
Last edited by a moderator:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
What's the formula in column I?

You could change that formula so it doesn't return an error value and then you wouldn't need the error handler.

Or you could check to see if the formula is returning an error, something like this.
Code:
For i = 2 To top
    With Range("I" & i + 57)
        .Formula = "=VLOOKUP(H" & i + 57 & ",F$59:G$" & top + 57 & ",2, FALSE)"
        .Font.Name = "Arial"
        .Font.Size = 8
    End With

    If Not IsError(Range("I" & i + 57)) = "MATCH" Then
        Range("H" & i + 57).Interior.ColorIndex = 7
    End If
Next i
 
Upvote 0
The formula in column I is the VLOOKUP which will see if the compared values match. I don't really know enough about the language to know a formula that will do the same thing but return a value if there is no match.

I used your code, but I'm still getting a runtime error at
If Not IsError(Range("I" & i + 57)) = "MATCH" Then

I feel like I was close with the first code, but for some reason, if it encountered one error, it wouldn't know how to act if it came across a second error
 
Upvote 0
Oops, forgot to remove the = "MATCH" bit.
Code:
    If Not IsError(Range("I" & i + 57)) = "MATCH" Then
        Range("H" & i + 57).Interior.ColorIndex = 7
    End If

If you really want to use On Error... then I would suggest something like this.
Code:
For i = 2 To top
    With Range("I" & i + 57)
        .Formula = "=VLOOKUP(H" & i + 57 & ",F$59:G$" & top + 57 & ",2, FALSE)"
        .Font.Name = "Arial"
        .Font.Size = 8
    End With

    On Error Resume Next

    If Range("I" & i + 57) = "MATCH" Then
        Range("H" & i + 57).Interior.ColorIndex = 7
    End If

    On Error Resume 0

Next i

That puts the error handling at the point in the code where the error is going to happen, it also avoids jumping out the loop and then jumping back - that kind of thing can make code hard to follow.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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