Vlookup - Unable to get the vlookup property of the worksheetFunction class

ExcelUser5005

New Member
Joined
Jan 23, 2019
Messages
4
Hi

Im getting error on attaining vlookup result.

Pls help


Code:
Sub vlokup()

Dim myLookupValue As String
Dim myFirstColumn As Long
Dim myLastColumn As Long
Dim myColumnIndex As Long
Dim myFirstRow As Long
Dim myLastRow As Long
Dim myVLookupResult As String
Dim myTableArray As Range
Dim WsRoute As Worksheet
Dim WsCT As Worksheet
Dim LVCol As Long
Dim LVRow As Long
    Set WsRoute = Sheet2
    Set WsCT = Sheet3
    
    LVRow = 2
    LVCol = 10
    myLookupValue = WsCT.Cells(LVRow, LVCol).Value
    
    myFirstColumn = 2
    myFirstRow = 1
    myColumnIndex = 2
  
    WsRoute.Activate
    
    myLastColumn = Application.WorksheetFunction.CountA(Range(Range("B1"), Range("B1").End(xlToRight)))
    myLastRow = Application.WorksheetFunction.CountA(Range(Range("B1"), Range("B1").End(xlDown)))
  
    With WsRoute
        Set myTableArray = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
    End With
    WsRoute.Range("BJ1").Value = WorksheetFunction.VLookup(myLookupValue, myTableArray, myColumnIndex, False)
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
That is the equivalent of #N/A error when =VLOOKUP formula in cell fails to find a match

Deal with it like this to allow VBA to keep running
Code:
    On Error Resume Next
    WsRoute.Range("BJ1").value = WorksheetFunction.VLookup(myLookupValue, myTableArray, myColumnIndex, False)
    If Err.Number > 0 Then
        On Error GoTo 0                    [I][COLOR=#006400] 'resets error code[/COLOR][/I]
        MsgBox "Value not found"
        
        [I][COLOR=#006400]'followed by alternative instruction[/COLOR][/I]
        [COLOR=#ff0000][I]'what should happen????[/I][/COLOR]
    End If
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,614
Office Version
  1. 365
Platform
  1. Windows
Another way without the error handler
Code:
Dim x As Variant
 x = Application.vlookup(myLookupValue, myTableArray, myColumnIndex, False)
If IsError(x) Then
   'do something
Else
   WsRoute.Range("BJ1").Value = x
End If
 

Watch MrExcel Video

Forum statistics

Threads
1,129,680
Messages
5,637,749
Members
416,982
Latest member
lisam77

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
Top