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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
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.
 
Upvote 0

Forum statistics

Threads
1,211,848
Messages
6,104,355
Members
447,902
Latest member
chriswebs23

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