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:
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!
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!