Excel 2010 Data Connection - Add Filter to Command Text

Begbie00

New Member
Joined
Nov 12, 2002
Messages
11
Two relevant workbooks (sitting in the same shared drive):

Historical Data.xlsx: with a table GLDATA, which includes a field cc
Template.xlsx: with a named range COSTCENTER

Question: How do I modify the Command Text in the Data Connections Properties dialog box (using either SQL or Table syntax) to only pull those records from GLDATA into Template.xlsx where cc = COSTCENTER

What have I tried so far

SQL Syntax: SELECT * FROM GLDATA WHERE GLDATA.cc = COSTCENTER

Error: Syntax Error in FROM clause.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
hi.

The cc value is text and needs delimiters - 'COSTCENTER'. These would not be required if cc was numeric.

SELECT * FROM GLDATA WHERE GLDATA.cc = 'COSTCENTER'

SELECT * FROM GLDATA WHERE GLDATA.cc = 123

regards
 
Upvote 0
Sorry, I wasn't clear. COSTCENTER is a named range (1 cell) in which the value of cost center exists. So your second example (123) needs to change based on a cell value, not be hard coded into the command text.

I thought there was a way to do this with "?".
 
Upvote 0
I didn't read the original post carefully. Sorry. You clearly wrote about the named range.

Yes, there is an approach with a ?. Separately there is also VBA to change the SQL.

I think the "?" approach is what you want. Exactly how it is implemented might be version specific. I'll investigate and reply later about Excel 2010. Basic idea is to edit the SQL however you can (normal Excel worksheet interface or via VBA. One way in VBA is the immediate window) so that the SQL has a ?. that would be

SELECT * FROM GLDATA WHERE GLDATA.cc = ?

A while since I've done this, it may instead be with bracket delimiters.

SELECT * FROM GLDATA WHERE GLDATA.cc = [?]

Having done that & refreshed the query you should be prompted for a value for the cost center parameter. At this stage there should be a choice to select the named range that you want and also to select for the query to refresh on every change of that cell.

For info, the separate way with VBA might have some event code run on change to the named range and it simply edits the SQL (text) to have the named range's value loaded.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

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