Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vba - Unable to get the vlookup to work

I run this set of codes and it returns an error:

Run-time error '1004' Unable to get the Vlookup property of the WorksheetFunction class.

I already have one vlookup in another sub routine. Is there anything wrong with this code ? I debug and the error appears at the policybox there.

Sub LinkPolicyNum()
Dim r As Integer
Dim policynum As Variant
Dim lookup_num As Range
Dim policybox As Variant


    r = ActiveCell.Row
    'Row number of the Selected Cell

    policynum = ActiveSheet.Cells(r, 3).Value

    Set lookup_num = ThisWorkbook.Sheets("PolicyDetails").Range("a1:z5000")

    policybox = Application.WorksheetFunction.VLookup(policynum, lookup_num, 3, False)
    'to match the policy number to the policy details

    MsgBox policynum
    MsgBox policybox



End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There appears to be nothing wrong with your code. You are seeing the result of what happens when you use the WorksheetFunction version of functions and no result is returned. Specifically, they throw an error and interrupt execution of the VBA. In this case, if you tried the same formula in the workbook instead of in VBA, you would get some form of error (#N/A or #VALUE! possibly).

If you want to prevent this from happening, the easiest thing to do is to change to using Application.VLookup instead of Application.WorksheetFunction.VLookup. Although there is no Intellisense to help with this function it behaves the same as the other except for error handling. If the non-WorksheetFunction version of a function has an error, it will return the error instead of throwing it. This allows you to check for an error and then carry on with your code.

If you think you should be finding a value with VLOOKUP here then you can start checking for mismatches between text/numbers and other things like that. I would check with formulas and not in VBA though.

Here is an example of using the other functional form and trapping the error.

Sub LinkPolicyNum()
    Dim r As Integer
    Dim policynum As Variant
    Dim lookup_num As Range
    Dim policybox As Variant

    r = ActiveCell.Row
    'Row number of the Selected Cell

    policynum = ActiveSheet.Cells(r, 3).Value

    Set lookup_num = ThisWorkbook.Sheets("PolicyDetails").Range("a1:z5000")

    policybox = Application.VLookup(policynum, lookup_num, 3, False)
    'to match the policy number to the policy details

    If IsError(policybox) Then
        'possibly do something with the "not found" case
    Else
        MsgBox policynum
        MsgBox policybox
    End If

End Sub

Reference on this issue: http://dailydoseofexcel.com/archives/2004/09/24/the-worksheetfunction-method/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...