Error using Application.WorksheetFunction.VLookup

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
275
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2011
Platform
  1. Windows
  2. MacOS
This is my first time trying to use VLOOKUP within VBA code, and I am not having any luck. I am getting a 1004 error: Unable to get the Vlookup property of the WorksheetFunction class.
I even found a similar question on this board where a user suggested adding some code to determine the specific error, but that is not working.

Here is my code:
VBA Code:
Function GetSheetOfLineNumber(LineNum As String) As String

Dim MyVar As Variant
Dim Rng As Range

'***This variable is for testing only
Dim Msg As String

Set Rng = ThisWorkbook.Sheets("LineNumbers").Range("D1:G379")

MyVar = Application.WorksheetFunction.VLookup(LineNum, Rng, 4, False)

'***This Select block is for testing only
Select Case CLng(MyVar)
    Case 2000: Msg = "#NULL!"
    Case 2007: Msg = "#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

'***This is for testing only
If Msg <> "" Then MsgBox "Worksheet Function Error:" & vbLf & vbTab & Msg, vbExclamation, "Error"

Set Rng = Nothing

GetSheetOfLineNumber = MyVar

End Function

The error is occurring on the Vlookup line, so it never gets to the Select statement to check the error.

The variable LineNum during my testing is equal to "277".

In Column D on the "LineNumbers" sheet, all cells contain formulas. However, the result of one of the formulas is definitely 277. (The formula is simply grabbing the value of another cell on a different sheet.) All cells in Column D are formatted as "General". Could that cause the error? (While 277 is the result of one of the cells, it's possible the result might not be numeric, which is why I am treating it as a String.)

I'm also not sure why the code above isn't working to at least show me the specific error. Any help would be greatly appreciated!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi mcomp72,

The crux of the problem is that LineNum is a string variable but it should be a numeric variable.

This works for me:

VBA Code:
Option Explicit
Function GetSheetOfLineNumber(LineNum As Long) As String

On Error GoTo ErrTrap
    
    Dim Rng As Range
    
    Set Rng = ThisWorkbook.Sheets("LineNumbers").Range("D1:G379")
    GetSheetOfLineNumber = Application.WorksheetFunction.VLookup(LineNum, Rng, 4, False)
    Set Rng = Nothing
    
Exit Function

ErrTrap:

    Set Rng = Nothing
    MsgBox "Worksheet Function Error:" & vbNewLine & "Error Number: " & Err.Number & vbNewLine & "Error Description: " & Err.Description, vbExclamation, "Error"
    
End Function
Sub TestFn()

    MsgBox GetSheetOfLineNumber(1)

End Sub

Regards,

Robert
 
Upvote 0
Hi mcomp72,

The crux of the problem is that LineNum is a string variable but it should be a numeric variable.

This works for me:

VBA Code:
Option Explicit
Function GetSheetOfLineNumber(LineNum As Long) As String

On Error GoTo ErrTrap
   
    Dim Rng As Range
   
    Set Rng = ThisWorkbook.Sheets("LineNumbers").Range("D1:G379")
    GetSheetOfLineNumber = Application.WorksheetFunction.VLookup(LineNum, Rng, 4, False)
    Set Rng = Nothing
   
Exit Function

ErrTrap:

    Set Rng = Nothing
    MsgBox "Worksheet Function Error:" & vbNewLine & "Error Number: " & Err.Number & vbNewLine & "Error Description: " & Err.Description, vbExclamation, "Error"
   
End Function
Sub TestFn()

    MsgBox GetSheetOfLineNumber(1)

End Sub

Regards,

Robert
Thanks for this. One potential issue I see with it is that sometimes LineNum might be something like this: "A315"

Obviously that's not a numeric value. Any suggestion on how I could change the code so that it would still work if LineNum was a non-numeric value?
 
Upvote 0
Best guess without seeing the data is that there is no exact match. You haven't used On Error Resume Next before the lookup line so your select case test will still fail.
 
Upvote 0
One potential issue I see with it is that sometimes LineNum might be something like this: "A315"

Try this slightly amended solution:

VBA Code:
Option Explicit
Function GetSheetOfLineNumber(LineNum As String) As String

On Error GoTo ErrTrap
    
    Dim Rng As Range
    
    Set Rng = ThisWorkbook.Sheets("LineNumbers").Range("D1:G379")
    GetSheetOfLineNumber = Application.WorksheetFunction.VLookup(IIf(IsNumeric(LineNum) = True, Val(LineNum), LineNum), Rng, 4, False)
    Set Rng = Nothing
    
Exit Function

ErrTrap:

    Set Rng = Nothing
    MsgBox "Worksheet Function Error:" & vbNewLine & "Error Number: " & Err.Number & vbNewLine & "Error Description: " & Err.Description, vbExclamation, "Error"
    
End Function
Sub TestFn()

    MsgBox GetSheetOfLineNumber(1)

End Sub
 
Upvote 0
Solution
That function will only work if it is a certainty that the data in the lookup range (Rng) will not change. As Rng is not a precedent in the function there is nothing to trigger calculation unless you make it volatile.

Looking at the function, the solution would be to scrap the UDF attempts and use the lookup formula in the cell instead. More efficient and less error prone.
 
Upvote 0
That function will only work if it is a certainty that the data in the lookup range (Rng) will not change. As Rng is not a precedent in the function there is nothing to trigger calculation unless you make it volatile.

Looking at the function, the solution would be to scrap the UDF attempts and use the lookup formula in the cell instead. More efficient and less error prone.
The function is never called directly from a worksheet, only via VBA code. In that instance, is it okay as is? Or would you still recommend scrapping it?
 
Upvote 0
In that instance it should be ok but using vba range find might be better depending on the main task.
 
Upvote 0

Forum statistics

Threads
1,215,019
Messages
6,122,707
Members
449,093
Latest member
Mnur

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