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

ExcelUser5005

New Member
Joined
Jan 23, 2019
Messages
9
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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,048
Latest member
81jamesacct

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