Solved

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

Posted on 2014-10-02
2
8,572 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

737 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