load vlookup results from another (closed) workbook into an open workbook using vba

twinwings

Board Regular
Joined
Jul 25, 2012
Messages
69
Hello,

So I am pretty sure this issue has come up before, but I can't seem to solve it using any of the past answers regarding similar situations.

What I would *like* Excel to do is.

vlookup data in cell A3, from a currently open workbook ---- and match it against data in a closed workbook in the following path, and return me the results as values.


The path for the workbook that has the data is this:
S:\a\b\c\d\e\f\g\warrantyL12.xlsx

Then, I would like excel to paste the result on my currently open workbook as values. So vlookup cell a3, from my current open workbook against data in warrantyL12.

Here's my code so far
Code:
Sub lookuptest()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Range1, myValue
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("S:\a\b\c\d\e\f\g\warrantyL12.xlsx")


Range1 = wb2.Sheets("data").Range("A:K")
ActiveCell = Application.WorksheetFunction.VLookup(A3, Range1, 11, False)


End Sub

On this code, I end up opening the file and the macro gets stuck at "ActiveCell".
I guess I never specified where I would like the results to be pasted, although as far as I can understand, ActiveCell is the cell I have last selected (and the cell I have last selected is where I would like my data to be pasted).


Eventually, I will end up having a worksheet that has about 100 unique cells in column A, and columns B to Z have data that are the results of vlookups of cells from Column A, but obviously each column has a different kind data.

Currently, all I do is select B3 to Z3, and double click on the corner upon which the formulas are effectively pasted down to the last row.

But sometimes I have several thousand rows, and it takes a long time for all the columns with all the vlookups to fill through. Furthermore, I just need the data as values so vlookups also take up space just by being active, untilI paste over as values.

In the past, I have seen macro enabled spreadsheets that with a click of a button, a bunch of data is filled out. The data is filled out in the same logic as a vlookup would fill it out based on data from the leftest column, but they are all pasted as values.


I understand this post is pretty convoluted, but I hope you guys get the idea.

Thank you for your help,
tw
 
Hi Joe,

Haven't had a chance to look at this until now.

Maybe I can explain again...


In my case

Workbook A, column A has fruits (think apples, oranges, mango).
Workbook B has the whole universe of foods including fruits (so meat, vegetables, fruits, etc).
Now column B in workbook B has the market price for each food.
However, I am only concerned for the foods (or fruits in this case) in column A workbook A.

I can easily do a vlookup where it returns me the price for each food in column A, workbook A, based on workbook B. However, if I have too many foods that I need to pull data for..Excel gets slow.

So is there a way, where I essentially accomplish the same result as I do now with vlookups, but without ever opening workbook B , and pasting the corresponding values in workbook A, as paste special values.



So far, iv managed to macro vlookup/paste-values until the last row --- but this is too ugly….



Any ideas?
Why do you believe that opening the workbook is causing slowness? Did you try the code I posted in post #6?
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I did,

it says out of memory at the Range (A:K)
Try replacing this:

Set Range1 = .Sheets("data").Range("A:K")

with this:

Set Range1 = intersect(.Sheets("data").Range("A:K"),.Sheets("data").UsedRange)
 
Upvote 0
Can you post a sample of range A:K in the workbook that's being opened by GetObject?


[TABLE="width: 857"]
<colgroup><col span="6"><col><col><col><col><col></colgroup><tbody>[TR]
[TD]pork[/TD]
[TD] [/TD]
[TD]xxx [/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1421.22[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]853.35[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]-99[/TD]
[/TR]
[TR]
[TD]cucumber [/TD]
[TD] [/TD]
[TD]yyy [/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1421.22[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]853.35[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]-100[/TD]
[/TR]
[TR]
[TD]apple[/TD]
[TD] [/TD]
[TD]zzz [/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]-29[/TD]
[/TR]
</tbody>[/TABLE]





and on my workbook that I am trying to fill out, column A has the following. so if I need column B from get object, then the workbook im trying to fillout would look like this


apple zzz
orange #N/A
mango #N/A


(assuming orange and mango are not found in the getObject document)
 
Upvote 0
[TABLE="width: 857"]
<tbody>[TR]
[TD]pork[/TD]
[TD][/TD]
[TD]xxx[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1421.22[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]853.35[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]-99[/TD]
[/TR]
[TR]
[TD]cucumber[/TD]
[TD][/TD]
[TD]yyy[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1421.22[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]853.35[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]-100[/TD]
[/TR]
[TR]
[TD]apple[/TD]
[TD][/TD]
[TD]zzz[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]-29[/TD]
[/TR]
</tbody>[/TABLE]





and on my workbook that I am trying to fill out, column A has the following. so if I need column B from get object, then the workbook im trying to fillout would look like this


apple zzz
orange #N/A
mango #N/A


(assuming orange and mango are not found in the getObject document)
Not very helpful w/o the column labels. If apple is in col A is zzz in col K?
 
Upvote 0
Not very helpful w/o the column labels. If apple is in col A is zzz in col K?

[TABLE="width: 661"]
<tbody>[TR]
[TD]a[/TD]
[TD]b[/TD]
[TD] c[/TD]
[TD]d[/TD]
[TD]e[/TD]
[TD]f[/TD]
[TD]g[/TD]
[TD]h[/TD]
[TD]i[/TD]
[TD]j[/TD]
[/TR]
[TR]
[TD]pork[/TD]
[TD="align: right"]1[/TD]
[TD]0[/TD]
[TD]12[/TD]
[TD]0[/TD]
[TD]1421.22[/TD]
[TD]0[/TD]
[TD]853.35[/TD]
[TD]xxx[/TD]
[TD]-99[/TD]
[/TR]
[TR]
[TD]cucumber[/TD]
[TD="align: right"]2[/TD]
[TD]0[/TD]
[TD]12[/TD]
[TD]0[/TD]
[TD]1421.22[/TD]
[TD]0[/TD]
[TD]853.35[/TD]
[TD]yyy[/TD]
[TD]-100[/TD]
[/TR]
[TR]
[TD]apple[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD]zzz[/TD]
[TD="align: right"]5[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]a[/TD]
[TD]b[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]apple[/TD]
[TD]zzz[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]orange[/TD]
[TD]#N/A[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]mango[/TD]
[TD]#N/A[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]

[/TD]
[/TR]
</tbody>[/TABLE]


zzz can be in any of the columns in workbook B...., however it is a unique type of data, so you wouldn't see zzz in column D for example. At the same time, the spreadsheet with all the foods has a fixed order in the way its columns are displayed. It is a report, that is generated in a monthly basis. So although Apple had zzz this month, it might have ccc next month, however both zzz and ccc will always be under column I

so in workbook A(the workbook which only has the fruits), I can easily specify in an absolute manner that column B, needs to vlookup data in column I in workbook B(the workbook with all the foods) - as the location of either column, in their respective workbook, will never change.


------------------------------------------------------------------------------

Another solution would be to have a macro that it searches for fruits in the master food spreadsheet, and if it finds a match (in this case apple), it returns me that entire row.

So once it finds apple, it returns me 3/0/0/0/0/0/0/zzz/0 (so the entire row that it matched).


At the same time, the next step I would like it to do, if it finds a match, it returns me only the specified columns. And these specified columns are not variable inputs by the user, they can be hard coded in the macro, just as long as it'snot the entire row.

So if this case, Apple was the match, and I have coded in my macro to return me the data from column B, f, I, and J, - then in my spreadsheet that only has fruits, the next 4 adjacent columns would fill out

Apple - 3 - 0 - zzz - 5 (see the ones that are underlined and italicized)
 
Upvote 0
The code you posted initially and that I modified in Post #6 does a lookup over the range A:K and returns column 11, that's column K. Now you say you want to return column I, that's 9 not 11 so you need to modify the code accordingly.
 
Upvote 0

Forum statistics

Threads
1,223,808
Messages
6,174,742
Members
452,580
Latest member
ruby9c

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