sql query from Excel using range

ofcabbagesandkings

New Member
Joined
Jan 31, 2012
Messages
2
I am given a spreadsheet with a column of item numbers that changes each week. I need to take those item numbers and compare them to the same data in another database. I can connect to the database AS400(DB2) fine and return single rows of data. Rather than using the three options from the query wizard: select by string, select by hard value or select by cell, is there a way to return multiple values such as A1:A75?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
hi

I'm not familiar with the three options described. Maybe you're in a newer Excel version than me? (Excel 2003)

Some thoughts.

  1. Within MS Query can you add another table? So, one table will be the AS400(DB2) and the other will be the range of cells A1:A75 (which I assume has a header in A1). Then join the tables on the common field and the returned dataset should be only the records from AS400(DB2) matching the entries in range A1:A75. Joining the tables should be possible in the GUI or otherwise edit the SQL. SQL could be like "SELECT whatever FROM AS400, ExcelRange WHERE AS400.commonfield = ExcelRange.commonfield". Or as below, you can make a string (via worksheet formulas maybe) and paste that to an "IN (list)" construction.
  2. Via the VBE (visual basic editor) edit the query tables SQL to suit. It might be like the SQL above except where I've written ExcelRange I think you'd need a fully qualified path. Or another way would be to loop through the range and create a string with all the values. If the entries are all numbers, the SQL clause will be like "SELECT whatever FROM AS400 WHERE AS400.filterfield IN (123, 345, 378, 382, 399)"
HTH
 
Upvote 0
Thank you FAZZA for your suggestions. To make it easy for the user I am forced to concatenate "or itmnum?" in the select statement, but I will try one of your suggestions for my own sake.
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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