Solved

How to make an Excel VBA vlookup when the tables are on another workbook?

Posted on 2014-10-02
2
7,375 Views
Last Modified: 2014-10-04
How would I go about making a macro that uses vlookup formulas when the tables are on another workbook?
If you look at the BHNRES-Unrecovered-Equipment-TEST VBA file on the 2nd tab it shows the results that I am looking for.
The macro I made is asking to open up the workbook where the tables are on...I do not want it to do this. Is there another to do this?

Thanks,
Jimi
BrightHouse-Item-Code-Reference.xlsx
BHNRES-Unrecovered-Equipment-1-59-TEST-V
0
Comment
Question by:Jimi Sherman
2 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
Comment Utility
It's easier to set up cross-workbook references when both files are open.  Once you create the VLOOKUP formulas correctly, Excel will retain the filepath of the referenced (and possibly closed) workbook in the formula.  Of course, if the source file is moved or deleted, this won't work.

So, the raw formulas in the "Example VBA Results" sheets should be (copied down):
C2: =VLOOKUP(A2,[BrightHouse_Item_Code_Reference.xlsx]Codes'!$A:$B,2,FALSE)
E2: =VLOOKUP(G2,[BrightHouse_Item_Code_Reference.xlsx]Codes'!$D:$E,2,FALSE)

If you wanted to create the formula without the source file ("BrightHouse_Item....") open, you would need to include the full drive:\filepath in front of the first square bracket, like so:
=VLOOKUP(A2,'C:\BrightHouseDirectory\Subdirectory\Subdir2\[BrightHouse_Item_Code_Reference.xlsx]Codes'!$D:$E,2,FALSE)
Also note the single apostrophe around the path, filename, and sheet name (needed because of spaces, but good practice).

===============
If you are trying to insert this in VBA, it's no different than inserting a regular formula, and since you have no quotation marks, that won't be an issue.  For example:
Range("C2").Formula = "=VLOOKUP(A2,[BrightHouse_Item_Code_Reference.xlsx]Codes'!$A:$B,2,FALSE)"

Open in new window

or with the full path,
Range("C2").Formula = "=VLOOKUP(A2,'C:\C:\BrightHouseDirectory\Subdirectory\Subdir2\[BrightHouse_Item_Code_Reference.xlsx]Codes'!$D:$E,2,FALSE)

Open in new window


Hope that helps,
Glenn
0
 

Author Closing Comment

by:Jimi Sherman
Comment Utility
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now