Solved

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

Posted on 2014-10-02
2
7,658 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
ID: 40358667
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
ID: 40361775
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

910 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

18 Experts available now in Live!

Get 1:1 Help Now