Seeking Advice: Efficient VBA Data Lookup in Table?

NigelTufnel

Board Regular
Joined
Apr 3, 2008
Messages
53
Office Version
  1. 365
Platform
  1. Windows
I'm working on a project in which I have to do a large number of lookups (thousands) on a series of large Excel lists (thousands of rows, perhaps ten columns each). I've done a lot of VBA programming, but not so much with databases/lists.

For each lookup, I will set three or four criteria, and a single record will be returned (if the data is good). I need to extract around five pieces of information for that unique record.

What's the best way to do this using VBA if I'm interested in calculation speed?

I put some code together that used Autofilter, and it seemed to run fine last night (it returned a result quickly and accurately). For reasons I cannot explain, today it's taking 15 seconds to impose each criteria on the database, so each lookup takes a minute long. I'm running Excel 2007 in Windows 7.

Is using Autofilter the best way to accomplish this task?

Another possibility is to manually set up some columns that return TRUE/FALSE for each criteria and another column that takes the AND across each row. I could then simply search for a TRUE in the AND column--this has got to take less calculation time than the Autofilter currently takes, although it will require more prep effort.

Any suggestions?

Thanks.
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
The way I would approach this first is to treat the table as the source for a SQL query. Excel supports SQL queries through the MS Query add-in that is accessed via Data tab | Get External Data group | From Other Sources drop down | from Microsoft Query button. It's also available in 2003 or earlier versions though the access is obviously different.

MS Query support parameterized queries where the query result is refreshed automatically when a parameter changes. For an intro see
Building and using a relational database in Excel (with a little help from MS Query)
http://www.tushar-mehta.com/excel/newsgroups/rdbms_in_excel/index.html

I'm working on a project in which I have to do a large number of lookups (thousands) on a series of large Excel lists (thousands of rows, perhaps ten columns each). I've done a lot of VBA programming, but not so much with databases/lists.

For each lookup, I will set three or four criteria, and a single record will be returned (if the data is good). I need to extract around five pieces of information for that unique record.

What's the best way to do this using VBA if I'm interested in calculation speed?

I put some code together that used Autofilter, and it seemed to run fine last night (it returned a result quickly and accurately). For reasons I cannot explain, today it's taking 15 seconds to impose each criteria on the database, so each lookup takes a minute long. I'm running Excel 2007 in Windows 7.

Is using Autofilter the best way to accomplish this task?

Another possibility is to manually set up some columns that return TRUE/FALSE for each criteria and another column that takes the AND across each row. I could then simply search for a TRUE in the AND column--this has got to take less calculation time than the Autofilter currently takes, although it will require more prep effort.

Any suggestions?

Thanks.
 
Upvote 0
Sorry, I mis-read the question. UPDATE queries are not suitable.

Parameterised queries, as per Tushar, might be fantastic. (Or similar via VBA if you want more control/flexibility.) It can be set up in a file separate to the data file: the data file can be mdb, xls, or whatever. If in the same file as the source data and the file name/path changes a little bit of VBA will help accommodate the changes (to the query connection and, if necessary, the SQL aka CommandText).

regards
 
Upvote 0
Tushar and Fazza--

Thanks for the detailed and thoughtful suggestions.

One (perhaps critical?) piece of information that I left out...

I don't plan to do more than one lookup of a single table in succession.

The nature of the task dictates that I find a piece of data in each of the "N" databases in succession for a particular operation, then find a single piece of data in each database for the next operation, etc.

Does this negate the advantage of using SQL?
 
Upvote 0
It isn't clear to me what is required, Nigel. I guess an SQL-type approach will still be good.

It might help to have an example.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

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