Can I use VBA to insert just the data found in a LOOKUP?

John3867

New Member
Joined
May 15, 2006
Messages
35
As silly as this sounds I'm curious.

Can I use VBA to automate runnng LOOKUP formulas, insert the data found into the cell, but not the formula into the cell?

I'm using a very large number of LOOKUP or vLOOKUP functions (+1000) on the spreadsheet and I would like to reduce its stored size but still get the data!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How big is the file that it is causing you a problem? It's almost always better to let XL do the stuff it was designed to do than have someone write VBA code that attempts to improve its performance.

FWIW, I just created a workbook with 2000 VLOOKUPs where the table was in another workbook and the lookup value was a randomly generated number. The file is 214KB and it calculates in the blink of an eye.
 
Upvote 0
The woorkbooks vary in size depending on the number of parts being tracked in the program. (from 1.5 mb to 2.6 mb) The file that is 2.6 mb takes a few minutes to run the vLookUps and Lookups. I think the total number of functions on the spreadsheet is around 6300. Pulling data from three different sources that are stored in shared folders.

Examples of the formulas I'm using are below:

=IF(ISERROR(VLOOKUP(E4:E466,INDIRECT.EXT("'\\Frg1\shared\DCShare\EcnReports\["&A4&"_RItemsPartsX.xls]"&A4&"_RItemsParts'!$A$2:$R$500"),3,FALSE)),"-",VLOOKUP(E4:E466,INDIRECT.EXT("'\\Frg1\shared\DCShare\EcnReports\["&A4&"_RItemsPartsX.xls]"&A4&"_RItemsParts'!$A$2:$R$500"),3,FALSE))

=LOOKUP(2,1/(('\\Frg1\shared\dcshare\EcnReports\EcnReports\All Tasks\[650 CA Durartion.xls]650'!$B$2:$B$1100=B4)*('\\Frg1\shared\dcshare\EcnReports\EcnReports\All Tasks\[650 CA Durartion.xls]650'!$C$2:$C$1100=CC4)),'\\Frg1\shared\dcshare\EcnReports\EcnReports\All Tasks\[650 CA Durartion.xls]650'!$E$2:$E$1100)

I would agree that if the VBA code would really only add to the work of getting it done it doesn't make sense to write it. So it seems as if leaving the functions in the cells is the best method to continue with.

Any suggestions?
 
Upvote 0
Addiionally, one file I'm looking in to would have 10,000 rows of data of which I'm paring down to just under 1,100. It would be nice to leave the file at 10,000 rows however when I do my Excel shuts downs when I try to save the file holding the 6,300 functions.
 
Upvote 0
These can definitely be cleaned up. I realize there are many here -- and I am sure some will chime in soon -- who like to cram in everything into as few cells as possible. Then, when there is a performance hit, they will use VBA to move the work out of XL. But a little common sense can go a long way to improving the workbook design without resorting to ever increasing gyrations.

I am curious. How do you use the first formula? Do you select 463 cells and array-enter it?

That formula can definitely be cleaned up. You are repeating the VLOOKUP needlessly. Since the VLOOKUP uses INDIRECT.EXT, you are duplicating that lookup needlessly. And, since the argument to the INDIRECT.EXT is a string concatenation, you are duplicating *those* calculations needlessly!

Put the VLOOKUP using the INDIRECT.EXT in one cell. Now, in the next cell, put the error handling fomula. Just that will significantly reduce the calculation demand on your system.

As far as the 2nd formula goes, do you ever have an instance where the 650 worksheet has duplicate matches on B4 and CC4 (in which case your formula gives you only the first match). If not, i.e., the 650 worksheet has only one possible match for each B and C combination, I would let SQL do the heavy lifting. I could be wrong but my guess is that it will be lightning fast. In an empty worksheet in your workbook, "import" the 650 data by selecting A1:E110 and entering a simple array formula ={range in the 650 worksheet}.

Save your workbook.

Now, open MS Query (Data | Import External Data > New database query...). Specify your source is a XL workbook. Next, specify the saved workbook as the source. Next, 'add' the worksheet with the imported range and worksheet with the data you want to look up to the query. Create a link between the columns B and C. To do so, click on the col. B name in the first table in the query and drag over to the column B name in the 2nd table. Do the same for column C.

Double click each item you want to add to the result. Probably, the column A and column E names.

Return to XL and put the result where desired.

Even if there are duplicate matches in the 650 worksheet, I suspect one can write a SQL lookup that gives you what you want but I cannot think of the solution of the top of my head.

The woorkbooks vary in size depending on the number of parts being tracked in the program. (from 1.5 mb to 2.6 mb) The file that is 2.6 mb takes a few minutes to run the vLookUps and Lookups. I think the total number of functions on the spreadsheet is around 6300. Pulling data from three different sources that are stored in shared folders.

Examples of the formulas I'm using are below:

=IF(ISERROR(VLOOKUP(E4:E466,INDIRECT.EXT("'\\Frg1\shared\DCShare\EcnReports\["&A4&"_RItemsPartsX.xls]"&A4&"_RItemsParts'!$A$2:$R$500"),3,FALSE)),"-",VLOOKUP(E4:E466,INDIRECT.EXT("'\\Frg1\shared\DCShare\EcnReports\["&A4&"_RItemsPartsX.xls]"&A4&"_RItemsParts'!$A$2:$R$500"),3,FALSE))

=LOOKUP(2,1/(('\\Frg1\shared\dcshare\EcnReports\EcnReports\All Tasks\[650 CA Durartion.xls]650'!$B$2:$B$1100=B4)*('\\Frg1\shared\dcshare\EcnReports\EcnReports\All Tasks\[650 CA Durartion.xls]650'!$C$2:$C$1100=CC4)),'\\Frg1\shared\dcshare\EcnReports\EcnReports\All Tasks\[650 CA Durartion.xls]650'!$E$2:$E$1100)

I would agree that if the VBA code would really only add to the work of getting it done it doesn't make sense to write it. So it seems as if leaving the functions in the cells is the best method to continue with.

Any suggestions?
 
Upvote 0
Thanks for taking the time to assist with this!

your question, "Do you select 463 cells and array-enter it?" Not exactly sure what you mean by this. But I enter the formula in cell H4 and then drag it down to H463.

First formula:
=IF(ISERROR(VLOOKUP(E4:E466,INDIRECT.EXT("'\\Frg1\shared\DCShare\EcnReports\["&A4&"_RItemsPartsX.xls]"&A4&"_RItemsParts'!$A$2:$R$500"),3,FALSE)),"-",VLOOKUP(E4:E466,INDIRECT.EXT("'\\Frg1\shared\DCShare\EcnReports\["&A4&"_RItemsPartsX.xls]"&A4&"_RItemsParts'!$A$2:$R$500"),3,FALSE))

I thought since the spreadsheet I need to get information from was on a shared directory and the file is not open on my desktop when the functions run that I needed to use INDIRECT.EXT as part of the vLOOKUP.

What I'm hearing you say is at the minimum remove the ISERROR "pretty factor" from each cell. Therefore the dredded #N/A will appear if it returns without success. I understand that.

If what I'm hearing you say for the MS Query is correct than I would not need use LOOKUP in formula 2 as it currently is written. I've never worked with MS Query so I'll have to spend some time following what you suggest.

The sheet 650 does contain duplicates. I use LOOKUP because I want to pull the first occurence of the MATCH. However there are flaws in this method and ideally that data would be cleaned to eliminates all duplicates prior to using LOOKUP.

Sheet 650 in its purest untouched form of extracted data is about 8 mb in size. So reducing its columns to only the data I need to MATCH and then pull would also increase performance. The actual size of sheet 650 would include 9,000 rows. Preferably I would not reduce it to the minimum of 1,100 (referenced in the formula) that includes the three catagories of data to MATCH against.

That said, I'll try to get through setting up the sheet to use MS Query.

Any other suggestions?
 
Upvote 0
Thanks for taking the time to assist with this!
No problem.
your question, "Do you select 463 cells and array-enter it?" Not exactly sure what you mean by this. But I enter the formula in cell H4 and then drag it down to H463.
In that case, look at your formula. Why is the first argument E4:E466? I believe it should be just E4.
First formula:
=IF(ISERROR(VLOOKUP(E4:E466,INDIRECT.EXT("'\\Frg1\shared\DCShare\EcnReports\["&A4&"_RItemsPartsX.xls]"&A4&"_RItemsParts'!$A$2:$R$500"),3,FALSE)),"-",VLOOKUP(E4:E466,INDIRECT.EXT("'\\Frg1\shared\DCShare\EcnReports\["&A4&"_RItemsPartsX.xls]"&A4&"_RItemsParts'!$A$2:$R$500"),3,FALSE))

I thought since the spreadsheet I need to get information from was on a shared directory and the file is not open on my desktop when the functions run that I needed to use INDIRECT.EXT as part of the vLOOKUP.
Yes, you need the INDIRECT.EXT unless you can do what I suggest below.
What I'm hearing you say is at the minimum remove the ISERROR "pretty factor" from each cell. Therefore the dredded #N/A will appear if it returns without success. I understand that.
Yes, but you don't have to live with the dreaded #N/A. What I am saying is this: In some empty column, say H, enter the =VLOOKUP(INDIRECT.EXT(...)) formula. This will contain the #N/A results for non-matches. Now, in the column where you want the real result, enter =IF(ISNA(H4),"-",H4). So, the column with the real results will *not* have any #N/A in it.

Edit: Depending on how many of these A$ & "_RItemsPartsX.xls" files you have, I would not be surprised if the largest drag on performance comes from having to look up all these different files.

Also, is it possible for you to do something about the structure of the files you are searching? If you merge them into a single table, you could use SQL here too. You would get *all* the desired results in a single step!

If what I'm hearing you say for the MS Query is correct than I would not need use LOOKUP in formula 2 as it currently is written. I've never worked with MS Query so I'll have to spend some time following what you suggest.

The sheet 650 does contain duplicates. I use LOOKUP because I want to pull the first occurence of the MATCH. However there are flaws in this method and ideally that data would be cleaned to eliminates all duplicates prior to using LOOKUP.
Before addressing the 2nd formula, clean up the first formula. If you still don't see acceptable performance then tackle the 2nd. [That said, if I were in your shoes, I would clean up both. But, that's me.]

Please clean up the 650 sheet. Your search will be so much easier to implement. Before my first post I tested my suggestion. And, in any case, I am a strong advocate of letting SQL do as much work as possible when it comes to data stored in relational tables.
Sheet 650 in its purest untouched form of extracted data is about 8 mb in size. So reducing its columns to only the data I need to MATCH and then pull would also increase performance. The actual size of sheet 650 would include 9,000 rows. Preferably I would not reduce it to the minimum of 1,100 (referenced in the formula) that includes the three catagories of data to MATCH against.
Why is this file so big? Does it also have the same kind of complicated formulas in it? If not, don't worry about the size. At least not yet. 8MB is not a big database. I would focus on the file cleaning and the SQL part first.
That said, I'll try to get through setting up the sheet to use MS Query.

Any other suggestions?
What? Isn't the above enough to keep you busy for the next...oh, several weeks? {grin}
 
Upvote 0
Quick Update: Eliminating the IF(ISERROR(... and reducing the range from ex: A4:A90 to just A4 for the vLOOKUP formulas reduced the processing time to SAVE the file by more than 50%.

I'll keep you posted.

John
 
Upvote 0
Thanks for the update. Glad to note things are working out.

Quick Update: Eliminating the IF(ISERROR(... and reducing the range from ex: A4:A90 to just A4 for the vLOOKUP formulas reduced the processing time to SAVE the file by more than 50%.

I'll keep you posted.

John
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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