How can I have a cell populated by a sql query?
For example, if a user manually input a part number into cell A1, cell A2 would populate by completing a query using A1 as a parameter.
Something like:
=select table.desc from table where table.partNo = 'A1'
Hope this makes sense.
Depends whether you want to populate cell by cell or by range.
I have used EPPlus along with C# to populate excel templates.
In our case the values are stored in database after the rule evaluation for each cell and then its either gets displayed on screen or exported to excel.
Its not a few lines of code which I can paste it here but you can try below:
1. Define rules with the sql criteria and definition that defines the coordinates of cell
2. Evaluate rules and persist the values
3. Display the values
Ping me if you need more help on this
Related
I am very new to SQL, I want to import data from SQL Server to Excel using this query
SELECT
Model, Factory, TargetTime, TotalEvalMins
FROM
AMSView
WHERE
WeekNumber = 45 AND WeekYear = 2021
I want to change the week number & year dynamically by taking user input from a cell.
Can anyone please suggest how to change the query?
Let's say the user values week & year in worksheet sample in A1,A2 , how can I write that query?
Since the amount of data is huge I must apply where while querying the data instead of applying filters in Excel.
Sorry for my bad English
Name each of your cells that you will use as parameters. This page describes the process.
Name a cell
1. Select a cell.
2. In the Name Box, type a name.
3. Press Enter.
For each cell containing a parameter for your query:
Select the cell
Use Data>Get & Transform Data>From Table/Range. This will open the PowerQuery Editor. You will see something like this:
Right-click the cell in row 1 in the grid in the Power Query Editor and select 'Drill Down'. This converts the query on your parameter cell to a named value which can be used in other queries. It looks like this:
Now in Excel, use Data>Get Data and create your query from the database. I created a sample table in a local SQL Server database called AMSView, then connected to it with the query text in your post. When finishing the query connection, select 'Transform' so the query opens in the PowerQuery Editor.
Now, use Home>Advanced Editor and edit as follows by replacing the fixed values in the WHERE clause with concatenated names of your parameter cells, converted to text. For brevity, I have only used one parameter. If you've used capital letters in your cell names, remember, the M language is case-sensitive, so the concatenated parameter name must have identical casing to the named value.
let
Source = Sql.Database("localhost", "StackOverflowTest", [Query="SELECT #(lf) Model, Factory, TargetTime, TotalEvalMins #(lf)FROM #(lf) AMSView #(lf)WHERE #(lf) WeekNumber = " & Number.ToText(week_number)])
in
Source
Once your query is finished, use Home>Close & Load to load the results to the workbook. Now, when your parameter cells change, you need only refresh the query (right-click, refresh) and the data will be filtered as required.
I'm trying to generate SQL queries based on data from an Excel sheet.
So I did write this formula
="INSERT INTO "&$B$1&" VALUES('"&Personnel!A1&"','"&Personnel!B1&"','"&Personnel!C1&"','"&Personnel!D1&"','"&Personnel!E1&"','"&Personnel!F1&"','"&Personnel!G1&"','"&Personnel!H1&"','"&Personnel!I1&"','"&Personnel!J1&"','"&Personnel!K1&"','"&Personnel!L1&"','"&Personnel!N1&"','"&Personnel!O1&"','"&Personnel!Q1&"','"&Personnel!R1&"','"&Personnel!S1&"')"
Which almost works except the fact that datas from Date Cells on my "Personnel" sheet gives me things like 24136 when I'm expecting 29-01-1966. How can i fix this and keep the original format ?
You can modify your query to change those values to their text equivalent using the TEXT function e.g.
="INSERT INTO "&$B$1&" VALUES('"&TEXT(Personnel!A1, "yyyy-mm-dd")&"'"
The problem is as follows: I have several tables in Excel with given number of raws and dynamic number of columns (each week a new column should be added, currently I'm doing it manually). I want to automate it and create a script that will extend every raw range to the next column (namely the range from A2 to C2 should become from A2 to D2), and so on (such that running script N times will result in extending a table to N columns further). By "extending" I mean extending formulas, since each cell in my tables contains any formula. Is there any way to do it via VBA?
I can't just record the corresponding macro because I have now idea how to specify that I don't want to link it with any specific range, but instead always extend just to one column right.
Any help and examples will be very appreciative.
You dont need VBA to do this. Use dynamic defined names and reference them in your formulas. For example, if you add a named range and add this in the refersTo dialog
=OFFSET($A$1,0,0,COUNTA($A:$A),COUNTA($1:$1)
your range will automatically expand from cell A1 (as long as there are no blank cells in column A or Row 1). You can then use that named range in your formulae.
More here http://www.excel-easy.com/examples/dynamic-named-range.html
I have a ODBC connection to a SQL database, which is returning my data to row A10 and down depending on a date criteria inserted into cell A1 and B1, along with a submit button.
What I would like to do is depending on the number of rows, so needs to be dynamic, could the rows be formatted with cell borders. That's all, simple, but checked on here and cant find anything like that. I don't want to use excel tables, unless I really have too.
Cheers
I have been trying to get the above data which is presented datewise in Column form to be pasted in the below format in Row form, but challenge is either 1 date at a time can be taken or either single cell reference needs to be given in each cell on other sheet, need some shortcut such that sheets data remains dynamic and datewise data is copied. Image shows it in one sheet, above data is required to be shown in below format in other sheet.
Thanks and Regards
Mandeep Goraya
For example, your table is like this:
Put this code into cell B8 then fill right:
=INDEX($B$2:$F$3,MOD(COLUMN(A1)-1+ROWS($B$2:$F$3),ROWS($B$2:$F$3))+1,1+INT((COLUMN(A1)-1)/ROWS($B$2:$F$3)))