Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
59 views
in Technique[技术] by (71.8m points)

How to use parameterized query in Excel using column as parameter?

I am trying to develop a spreadsheet that can locate corresponding records in an external data source. So, let's say I have Column A with a list of identity values. I want to develop Column B, which perhaps shows a count of rows in the table with that value. Something like:

A              B
758348    "=SELECT COUNT(*) FROM MYTABLE WHERE IDVALUE=$A$1"
173483    "=SELECT COUNT(*) FROM MYTABLE WHERE IDVALUE=$A$2"

... and so on. So, I thought I would use a parameterized query (where IDVALUE=?), but that prompts me to input the parameter value, not use the value from the cell to the left. Is there any way I can do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would use a parameterized query (where IDVALUE=?), but that prompts me to input the parameter value

From what you mentioned I understand you are using MS Query and you need to follow the following steps

Steps to make a parameterized query in Excel use a cell value as parameter

  1. First go to the Data tab in Excel and Select MS Query under External Data Sources

MS Query

  1. A pop up appears asking you to choose data source. Select the data source or add a new data source and select it. Make sure to uncheck Use the query wizard to create or edit queries it is not very useful in most cases

Data Source

  1. Then a pop up appears asking you to add table select any table for now. It is better to select a table with less rows.

add table

  1. Then a Query window appears with the table you have selected and its data

query window

  1. Then click on the view menu and uncheck tables. This removes the graphically representation in query window and makes it easier to alter you sql query

View menu

  1. Click on the SQL button below the format menu in query window. A pop up comes where you can edit the query as you wish. Make sure to add question mark(s) for the parameter(s) you want.

edit query

  1. Then a pop up appears ask you for parameter value(s) enter some valid value for now

parameter

  1. Then the query window shows the result of query based on the parameter entered

result

  1. Click on the exit button below the view menu in query window. Then the query window closes and Import Data pop up comes asking where in excel to display the result. Choose accordingly

import

  1. Then click on Properties button on left before you click OK. A new pop up appears with the default usage tab selected.

Properties

  1. Make any changes needed in this tab and click on Definition tab. Click on Parameters button in bottom of the pop up next to edit query button

Definition

  1. Parameters pop up comes displaying the parameters used in the query. Here you must select the Get the value from following cell radio button to select the value of cell as the parameter for the query. Click OK and you should be done.

    parameter value


This method is for people with no VBA experience. If you know VBA then refer to this answer to achieve the something similar using VBA.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...