Trouble with VLookup result not matching existing value

teachman

Active Member
Joined
Aug 31, 2011
Messages
321
Hello, I have a problem with VLookup in VBA scripts.
I have a named range that contains values with both letters and numbers. Such as:

VYVIV
ABAB

The list does not contain the value AAAA.

CHALLENGE: The user enters four characters in B19 through B25, 1 character in H19 through H25, a 1 or 2 digit number in K19 through K25. When concatenated they create a 4 to 7 character string. I need to validate this concatenated string against the named range containing a list of string values. I.e. VYVIV, ABAB, etc. The list does not contain the string AAAA.

The code snippet I have come up with is below:

Code:
Dim NPSym As String, res As String

For r = 19 To 25 Step 1
    NPSym = Range("B" & r) & Range("H" & r) & Range("K" & r)
    On Error Resume Next ' The error is always code 1004
    res = Application.WorksheetFunction.VLookup(NPSym, Range("NPSymbol"), 1, False) 
' Now compare res to NPSym
' Since I am looking for something that shouldn't exist, hence the 1004 error, I do the following
    If NPSym <> res Then
        Err = Err + 1
        stream.WriteLine "Error " & Err & ":  National Plant Symbol in row " & r & ", " & NPSym & ", does not exist in NASIS Plants table."
        FatalErr = FatalErr + 1
        stream.WriteLine "Fatal Error " & FatalErr & ":  The import into NASIS will fail if this error is not corrected."
    End If
Next r

My problem is the variable Err gets reset to zero once the 'On Error Resume Next' line is processed, but not the FatalErr variable. This screws up the error count from this point in the code on.

WHY does it do that?

Is Err some kind of semi-reserved word? What other problems can this cause? I ask these two questions because I have almost thirty forms that use Err and FatalErr to keep track of the number of errors for an Error Log report. I'm going to have to come up with something less than intuitive if this is a semi-real issue.

By the way, in my test error check routine I was able to pass the number of errors prior to the 'On Error...' statement around to after this bit of code. But, it is less than elegant.

Thanks for your help.

George Teachman

Thanks,
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Dim res as Variant then use

Code:
res = Application.VLookup(NPSym, Range("NPSymbol"), 1, False)

Err is a reserved keyword - you cannot use it like that.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,593
Messages
5,838,257
Members
430,536
Latest member
Manoj Gaidhankar

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
Top