I have a sheet with 2 columns. I need to CONCATENATE the two cells within each row to create a large WHERE statement in the SQL based off every row. For example:
Where A1 = 'B1' and A2 = 'B2' etc etc.
What do you suggest is the best method to do this? I need to do this across many sheets. Originally I was going to do something like this: C1=CONCATENATE(A1," = ","'",B1,"'") across every row, then CONCATENATE those outputs as well (C1,D1 etc) but just wondering if there are any other options? Would using VBA be easier?
No need to use any functions.
You may do like this,
assuming your excel sheet is like:
| A | B | C | D |
1 | a1 | b1 | | |
2 | a2 | b2 | | |
3 | a3 | b3 | | |
4 | a4 | b4 | | |
Insert one new row between A and B, and write =' in cell B1 and drag that cell to COPY cell value upto total number of your rows. Similarly write ' and in Cell D1 and do same, so it will be like this.
| A | B | C | D |
1 | a1 | =' | b1 |' and|
2 | a2 | =' | b2 |' and|
3 | a3 | =' | b3 |' and|
4 | a4 | =' | b4 |' and|
Now, Copy paste these cells to Notepad++ and replace TAB and \n by a space (new lines)
So, now you should get string like,
a1='b1' and a2='b2' and a3='b3' and a4='b4' and
You just have to edit minor thing, place this to your query and remove last and
Related
I have written a program to download data from an API, and parse the json response into an excel (xlsx) sheet in multiple worksheets.
However, depending on user's choice on what attributes to be saved, there can be empty columns in the excel. Is there any way to delete empty columns from all worksheets in the excel file?
I can do this through pandas, however, that is not possible in this case, as pandas and openpyxl library breaks the GUI based on the python script?
Is there any other way to implement this?
Edit:
Current Output:
+----------+----------+--+----------+----------+
| Column A | Column B | | Column D | Column E |
+----------+----------+--+----------+----------+
| A1 | B1 | | D1 | |
| A2 | | | D2 | |
| A3 | B3 | | | E3 |
+----------+----------+--+----------+----------+
Desired Output:
+----------+----------+----------+----------+
| Column A | Column B | Column D | Column E |
+----------+----------+----------+----------+
| A1 | B1 | D1 | |
| A2 | | D2 | |
| A3 | B3 | | E3 |
+----------+----------+----------+----------+
Current Code for removing blank columns:
excel_file = pd.ExcelFile(path, engine='openpyxl')
df = pd.read_excel(excel_file,header=None, sheet_name=None)
writer = pd.ExcelWriter(finalpath,engine='openpyxl')
for key in df:
sheet= df[key].dropna(how="all").dropna(1,how="all")
sheet.to_excel(writer, key,index=False, header=False )
writer.save()
Need to replace this code with something which don't references pandas library.
I would like to get reference for row from another cell but fix column.
For example:
This is normal way. In cell A1 will be YES
| - | A | B |
| -- | -- |-- |
| 1 | =B1|YES|
| 2 | 1 |NO |
I'd like to chage number 1 in =B1 to be read from cell A2. something like
-
A
B
1
=B(A2)
YES
2
1
NO
Why? Because then i will change number in cell A2 from 1 to 2 then will change cell A1 from YES to NO
You can use INDEX formula to get the desired results like below in cell B2.
=INDEX(C:C,A2)
What I'm trying to do is a little complex but I think it's doable in Excel.
I have two worksheets in a workbook on sheet one I have this...
| Code1 | Code2 | Code3 | Code4 |
| BA1 | xxxxx | xxxxx | |
| BA2 | xxxxx | xxxxx | |
| BA3 | xxxxx | xxxxx | |
And on the second sheet...
| CodeA | CodeB | CodeC | CodeD |
| BA1 | 1 | date | text |
| BA3 | 1 | date | text |
| BA1 | 2 | date | text |
| BA2 | 1 | date | text |
| BA1 | 3 | date | text |
| BA3 | 2 | date | text |
| BA2 | 2 | date | text |
What I want to do is lookup Code1 on sheet one and find it in the second sheet in CodeA then find the highest CodeB for CodeA and then concatenate CodeC and CodeD and place them on Sheet one in Code4.
I hope that makes sense, Thanks for any advice.
I think I understand. Does this look correct?
Sorry for the swedish formulas but it's an array formula that you add with CTRL+SHIFT+ENTER.
The formula in english is:
{=MAX(IF(Data=A2,CodeB;-1))}
And the named range Data is Column H and I, and CodeB is Column I.
If it does not find the value it returns -1
Sorry noticed now that I only did half of the job.
Make another named range called Table that spans column I to K (Code B -> Code D).
And in column code3 add this formula:
=Vlookup(B2,Table,2,false)
And in code4:
=Vlookup(B2,Table,3,false)
And you should get:
This should find the results you are looking for.
This is an array formula so you will need to press CTRL+SHIFT+ENTER once you have entered it into the formula bar, this will have to done for every formula you add to the column.
As it is an array formula I have only written it to reference rows 1 to 18, you will need to update all references to include you last row.
Columns titled CODE1(to 4) are on the first sheet (Sheet 1)
Columns titled CODEA(to D) are on the Second sheet (Sheet 2)
=CONCATENATE(VLOOKUP(CONCATENATE(A2,MAX(IF(Sheet2!A:A=A2,Sheet2!B:B,-1))), CHOOSE({1,2},Sheet2!A1:A18 & Sheet2!B1:B18, Sheet2!C1:C18 ),2,0)," ",VLOOKUP(CONCATENATE(A2,MAX(IF(Sheet2!A:A=A2,Sheet2!B:B,-1))), CHOOSE({1,2},Sheet2!A1:A18 & Sheet2!B1:B18, Sheet2!D1:D18 ),2,0))
If you do not require a space between the dates, just remove " ", from the middle of the formula.
What I want to do might be better achieved with a database, however I have very limited experience with them, and only have to change the information infrequently.
What I have is a sheet where each row has 8 cells of relevant data.
What I want to do in another sheet is to enter a number into 1 cell, and have a group of cells below use that number to reference data in the other sheet.
For example, in Sheet1 I could have the following (fig 1):
| A | B | C | D | E | F | G | H
-----+-----+-----+-----+-----+-----+-----+-----+-----
101 | Dep | 700 | Sta | 100 | Sta | 300 | Dep | 900
What I want to achieve in sheet 2, by typing the row number into 1 cell, is to have the data in those 8 cells copied below, for example (fig 2):
| A | B | C | D |
-----+-----+-----+-----+-----+
1 | "Row Number" |
-----+-----+-----+-----+-----+
2 | =A# | =B# | =D# | =C# |
-----+-----+-----+-----+-----+
3 | =E# | =F# | =H# | =G# |
-----+-----+-----+-----+-----+
And yes, I am aware those formulae above do not reference the other sheet - this was to save space.
Which, if using the example row above, should look like this (fig 3):
| A | B | C | D |
-----+-----+-----+-----+-----+
1 | 101 |
-----+-----+-----+-----+-----+
2 | Dep | 700 | 100 | Sta |
-----+-----+-----+-----+-----+
3 | Sta | 300 | 900 | Dep |
-----+-----+-----+-----+-----+
So, in that example above (fig 3), what do I need to put in as a formula in cells A2-D2 & A3-D3 to automatically use the number in A1 as part of the cell reference to print the data from the other sheet.
Does that make sense? I hope so because I have over 300 lines to enter into my 1st sheet and another 70 lines x 7 blocks of columns on the second sheet.
Lastly I just want to say I want to avoid programming languages, like VBA, wherever possible.
Check out the INDIRECT() function.
For cell A2 in your example on the second sheet, enter:
=INDIRECT("Sheet1!"&"A"&$A$1)
Expand this formula to the apply to other target cells by changing the "&"A" portion to reference columns B, C, D, etc. from Sheet1 as needed in your grid per the following example:
=INDIRECT("Sheet1!"&"B"&$A$1)
=INDIRECT("Sheet1!"&"C"&$A$1)
=INDIRECT("Sheet1!"&"D"&$A$1)
These formulas will reference your selected "Row Number" in cell A1 and pull the related data from Sheet1 into Sheet2.
You can do this using the INDIRECT function
Returns the reference specified by a text string.
References are immediately evaluated to display their contents.
Use INDIRECT when you want to change the reference to a cell within a
formula without changing the formula itself.
http://office.microsoft.com/en-gb/excel-help/indirect-HP005209139.aspx
I have a sheet that do a heavy calculation, with sums, factos and custom functions, that results in a single cell. Imagine a simple series of sums:
| A |
---------
1 | 39 | → This is my variable, everything else is fixed
2 | 800 |
3 | 200 |
4 | =Sum(A1:A3) |
In another sheet, I will like to get the result value of the formula on the Sheet1 A3 cell, however I will like to campare changes on one variable of that sheet, the Sheet1 A1 for that purpose
| A | B |
--------------------------------------------
1 | =X(Sheet1.A4, B1, Sheet1.A1) | 40 |
2 | =X(Sheet1.A4, B1, Sheet1.A1) | 50 |
3 | =X(Sheet1.A4, B1, Sheet1.A1) | 100 |
4 | =X(Sheet1.A4, B1, Sheet1.A1) | 200 |
5 | =X(Sheet1.A4, B1, Sheet1.A1) | 1000 |
That will result in:
| A | B |
------------------
1 | 1041 | 40 |
2 | 1051 | 50 |
3 | 1100 | 100 |
4 | 1200 | 200 |
5 | 2000 | 1000 |
So my question is, X function exist? how can I make it work that way? I Want to get the result of formula on the cell Sheet1.A3, however I want also for that case change the value of the A1 cell to get a different result.
Will appreciate any hint.
I would do this by having another sheet (or another data range in the same sheet) containing the possible values for A1 that you want to test, and then write a small VB macro that will:
Replace Sheet1:A1 with each of those values in turn
Recalculate Sheet1
Copy desired values (A1 and A3) into the relevant cells in Sheet2.