On Error GoTo is causing data to be looped over twice

brewitbold

New Member
Joined
Oct 13, 2016
Messages
5
Hello!


This is my first time using an On Error GoTo solution within a macro and I cannot find any documentation online to help me with an issue I am running in to. I think it's an issue of where in the loop I have placed the error handling line.

The snippet of code below performs a lookup and stores either a 1 or a 2 in a variable called "State." If the lookup value does not exist on the second sheet, then then "State" should be "Not Found. Currently what is happening is that a record that is NOT on the second sheet initially returns "Not Found" (yay!) and then the code loops back up to the if statement and overwrites the result to "2."

My thought was that the error handling would simply produce "not found" and then move to the next row.

I have commented out the actual vlookup, as it is long and does not seem to be impacting the actual error (I've tested it and it returnes accurate values 100%)




Code:
For i = 2 To lastRow
    lookupValue= 'Returns the value of a Vlookup from another sheet.

On Error GoTo EmpNotFound:
     If lookupValue= "string1" Then
     state = "1"
     
     Else
     state = "2"

End If

Next




EmpNotFound:
    state = "Not Found"
    Resume Next




Thank you for the assistance and collective brain power that this site attracts!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
If the lookup value can't be found then your error handler sets state to a string (OK so far), but the resume next then sends that string back to the If-Then construct and since the string "Not Found" is not equal to "String1" the Else line changes state to a 2.

Maybe easier to do something like this:
Code:
For i = 2 To lastRow
    lookupValue= 'Returns the value of a Vlookup from another sheet.
    If IsError(lookupValue) Then GoTo NX
     If lookupValue = "string1" Then
     State = "1"
     
     Else
     State = "2"

End If
NX:
Next

'rest of code
 
Upvote 0
I implemented this code fix and I now get "run-time error 1004: Unable to get the vlookup property of the worksheetfunction class" on the vlookup line.
 
Upvote 0
I implemented this code fix and I now get "run-time error 1004: Unable to get the vlookup property of the worksheetfunction class" on the vlookup line.
Forgot a line - try this:
Code:
For i = 2 To lastRow
       On Error Resume Next
    lookupValue= 'Returns the value of a Vlookup from another sheet.
    If IsError(lookupValue) Then
       State = "Not found"
       GoTo NX
       End If
     If lookupValue = "string1" Then
     State = "1"
     
     Else
     State = "2"

End If
NX: On Error GoTo 0
Next

'rest of codeEnd Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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