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,
 

Some videos you may like

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

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.
 

Watch MrExcel Video

Forum statistics

Threads
1,113,903
Messages
5,544,974
Members
410,647
Latest member
LegenDSlayeR
Top