Unable to get the VLookup property

Westbury

Board Regular
Joined
Jun 7, 2009
Messages
139
Within a longer macro I've included the following


Dim readingdate As String
Dim yrstart As Date
yrstart = Application.WorksheetFunction.VLookup(readingdate, Sheets("Lookups").Range("B7:E19"), 4, False)

but it returns an error "Unable to get the VLookup propertry of the Worksheetfunction class".

The VLookup works as an spreadsheet function. What's stopping it working in VBA?

thanks
Geoff
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello Geoff,

The WorksheetFunction in VBA does not return the error code that occurs like Excel does. Generally the message you are seeing means you have an #N/A error. If readingdate is not the first column in the table array (B7:E19) of your VLookup this will cause the error.

If you use Application.VLookup and assign a Variant variable as the return value, you will get an error code that can be translated into the Excel error.

Rich (BB code):
Dim Msg As string
Dim x As Variant
    x = Application.VLookup(readingdate, Sheets("Lookups").Range("B7:E19"), 4, False)
        Select Case x
            Case Is = "Error 2000": Msg = "#NULL!"
            Case Is = "Error 2007": Msgr = "#DIV/0!"
            Case Is = "Error 2015": Msg = "#VALUE!"
            Case Is = "Error 2023": Msg = "#REF!"
            Case Is = "Error 2029": Msg = "#NAME?"
            Case Is = "Error 2036": Msg = "NUM!"
            Case Is = "Error 2042": Msg = "#N/A"
        End Select
    If Msg <> "" Then MsgBox Msg
 
Last edited:
Upvote 0
Leith,

thanks for this post.

I've added the script and it returns a run time error 13 type mismatch and highlights the

Case Is = "Error 2000"

Geoff
 
Upvote 0
Hello Westbury,

Here is updated and tested macro code.

Code:
Sub TestIt()


    Dim Msg As String
    Dim x   As Variant
    
        x = Application.VLookup("Forecaster", Range("B2:E3"), 4, False)
        
        Select Case CLng(x)
            Case 2000: Msg = "#NULL!"
            Case 2007: Msgr = "#DIV/0!"
            Case 2015: Msg = "#VALUE!"
            Case 2023: Msg = "#REF!"
            Case 2029: Msg = "#NAME?"
            Case 2036: Msg = "NUM!"
            Case 2042: Msg = "#N/A"
        End Select
        
        If Msg <> "" Then MsgBox "Worksheet Function Error:" & vbLf & vbTab & Msg, vbExclamatioon, "Error"
        
End Sub
 
Last edited:
Upvote 0
I've run the updated code and it returned Worksheet function error #N/A.

What does this mean?

Geoff
 
Upvote 0
Hello Geoff,

The error is saying the value in readingdate is Not Available, i.e. this value was not found in the first column of your search range "B7:E19".
 
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,695
Members
449,331
Latest member
smckenzie2016

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