0 votes
in VBA by Beginner (2 points)

How do you use Excel VBA vLookup to reference another Workbook using VBA? I want to lookup a whole list of staffs instead of one by one. I've tried to Google it but I don't understand the recommendations. Here's what I have so far:

Sub vLookupAnotherWorkbook()

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 Long
    
    Dim myTableArray As Range
    
    myLookupValue = "Raymond Allen"
    myFirstColumn = 2
    myLastColumn = 3
    myColumnIndex = 2
    myFirstRow = 6
    myLastRow = 305
    
    With Workbooks("Excel VBA VLookup").Worksheets("VBA VLookup")
        Set myTableArray = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
    End With
    
    myVLookupResult = WorksheetFunction.vlookup(myLookupValue, myTableArray, myColumnIndex, False)
    
    MsgBox "Sales by " & myLookupValue & " are " & Format(myVLookupResult, "#,##0")

End Sub

 

1 Answer

0 votes
by Skilled (749 points)
edited by

WorksheetFunction.vlookup(...) should be WorksheetFunction.VLookup(...) but that is probably not the problem. 

Try changing Dim myVLookupResult As Long to Dim myVLookupResult As Variant but if that doesn't work, please describe the error message.

Also, make sure "Raymond Allen" matches an entry in your table EXACTLY; be careful with extra space characters.

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...