Concatenating Fields in Excel - excel

I have an Excel Document that has Table Names in Column A and Column Names in Column B.
A1 B1
Account Account_ID
I can get the syntax correct.
The formula is:
=CONCATENATE("Select '",$A$1,"','",B1,"',",B1,", count(*) From ",$A$1, " group by '",",B1,";")
The output that I get is:
Select 'ACCOUNT','ACCOUNT_ID',ACCOUNT_ID, count(*) From ACCOUNT group by 'ACCOUNT','ACCOUNT_ID',ACCOUNT_ID;
The output that I need is:
Select 'ACCOUNT','ACCOUNT_ID',ACCOUNT_ID, count(*) From ACCOUNT group by ACCOUNT_ID;
Any help would be greatly appreciated.

Try concatenation using the ampersand.
You can link together string literals and cell contents as such.
="String1" & "'" & "String2"
will result in
String1'String2
So to try and build sql statements from excel cells, lets say you have an excel sheet with tables and fields
A B C
1 Table1 Column1 Column2
2 Table2 COlumn1 Column2
You can build sql statements from those values
="SELECT 'Table1', " & B1 & ", " & C1 & " FROM " & A1
="SELECT 'Table2', " & B2 & ", " & C2 & " FROM " & A2
Will yield
SELECT 'Table1', Column1, Column2 FROM Table1
SELECT 'Table2', Column1, Column2 FROM Table2
The quotes specify Strings and the apersand (&) joins them together. Anything outside a set of strings will be evaluated.
For example a string with a bunch of spaces could be trimmed and joined
= TRIM(A1) & " Trimmed"
would yield
Table1 Trimmed
Your particular example would look like so
="SELECT '" & A1 & "', '" & B1 & "', Account_ID, COUNT(*) FROM " & A1 & " GROUP BY " & B1

Related

Concatenate in excel with special characters

This is my excel table
state City Name area type
GUJARAT Abdasa City
GUJARAT Adalaj City
GUJARAT Ahwa City
I want to create this(in the row next to area type of course)
('GUJARAT','Abdasa','City'),
For that I have tried following formulas
=CONCATENATE("('"+A2+"','"+B2+"','"+C2+"'),")
=CONCATENATE("('" & A2 & "','" & B2 & "','" & C2 & "'),")
I don't know much about Excel is this possible?
You don't need to use both CONCATENATE() and & operator - they do the same thing.
If you use &, write:
="('" & A2 & "','" & B2 & "','" & C2 & "'),"
If CONCATENATE(), write:
=CONCATENATE("('",A2,"','",B2,"','",C2,"'),")

string text with blank cells

I am trying to string together text from cells across by row when some of the cells are blank. I also want to have commas separating the text from each cell but cannot figure out how to only have commas put in when there is data in a cell.
I have tried using variations of concatenate and stringing the text using A1&B1&C1 but nothing is returning the data in the format I want.
Is there a combination of nested formulas I can use that will return the data in the format I want?
Here's a VBA solution I had before the newer versions:
Public Function JOINCELLS(ByVal ref As Range) As String
Dim c As Range
For Each c In ref
If Len(c) > 0 Then
JOINCELLS = JOINCELLS & c & ", "
End If
Next
JOINCELLS = Left(JOINCELLS, Len(JOINCELLS) - 2)
End Function
Then, it's just =JOINCELLS(A1:D1).
The formula below could do the trick. Concatenate the val The trim would remove spaces from the ends, and the substitute would swap the space for the comma(space).
=SUBSTITUTE(TRIM(A1 & " " & B1 & " " & C1 & " " & D1), " ", ", ")
Edit from the comments below - you could update the spaces between the quotes to be any character that does not appear in your cells. You would just have to the substitute formula to match. If you don't use the | character for example:
=SUBSTITUTE(TRIM(A1 & "|" & B1 & "|" & C1 & "|" & D1), "|", ", ")

Paste values from 3 cells into one cell with delimiters in excel

I have the following data where A to C are the columns:
A B C
-5.274 -20.63 9.251
where each number is in a different cell.
I want to combine these numbers in the following way and paste them into a new cell (those of column D)
-5.274 (-20.63 − 9.251)
How can I do this?
You can't copy and paste them like that, but you can very easily:
D2: =A2 & " (" & B2 & " - " & C2 & ")"
To keep the formatting:
=TEXT(A1;"0.00")& "(" & TEXT(B1;"0.00") & " " & TEXT(C1;"0.00") & ")"

Concatenate column text heading if 2 column totals equal

I have three columns, each with a heading lets call them: column a, column b column c. Each column has 10 questions which total at the bottom.
IF the total for 2 columns equal then concatenate the column headers together and insert an "or" in between.
i.e. if column a = 9 and column b = 9 and column c = 6, then concatenate column a or column b
The result to read: column a or column b
nb: the "or" in the middle of the two concatenated text is important
the formula I am using concatenates the text but does not add the "or" in-between the text.
=CONCATENATE(IF(MAX(A10:C10)= A10, "COLUMN A", ""), IF(MAX(A10:C10)= C10, "COLUMN B",""), IF(MAX(A10:C10)= C10, "COLUMN C",""))
Concatenate Query
This is one way of doing what you've asked using nested IF and AND statements
Assuming the sum of each column is in row 12, and the column headings are in cells A1, B1 and C1
=IF(AND(A12=MAX(A12:C12),B12=MAX(A12:C12)),A1 & " or " & B1, IF(AND(A12=MAX(A12:C12),C12=MAX(A12:C12)),A1 & " or " & C1, IF(AND(B12=MAX(A12:C12),C12=MAX(A12:C12)),B1 & " or " & C1,"")))
For the general case where if you want to know if all three columns are equal, or if only one of them contains the max value, then expanding the IF statement gives
=IF(AND(A12=B12,B12=C12),A1 & " or " & B1 & " or " & C1,
IF(AND(A12=MAX(A12:C12),B12=MAX(A12:C12)),A1 & " or " & B1,
IF(AND(A12=MAX(A12:C12),C12=MAX(A12:C12)),A1 & " or " & C1,
IF(AND(B12=MAX(A12:C12),C12=MAX(A12:C12)),B1 & " or " & C1,
IF(A12=MAX(A12:C12),A1,IF(B12=MAX(A12:C12),B1,C1))))))

Excel: Formula Returning from conditional

How to return formula of other cell (column L) if choose item in column A.
Example:
if answer, then return =I5 & " " & J5
if foo, then return '=I7 & " " & J7 & " " & K7 & " x " & L7
I want to return the formula instead of the result.
Put this function in a public module
Function GetFormula(rng As Range) As Variant
GetFormula = rng.Formula
End Function
I think this is what you want. There are several options for how you want that formula returned such as R1C1 style.
Edit
Oh I think I get what you want more. Ok to apply the formula you have in your blue table to whatever cell you want in column F you can call the Indirect function.
So for example if you want to apply the formula '=I7 & " " & J7 & " " & K7 & " x " & L7 to your column F then call =INDIRECT(I7 & " " & J7 & " " & K7 & " x " & L7). In your column I don't include the = sign. it will make it a bit easier.
You can use Vlookup to find where this should be applied by using your lookup item in column A in your reference table (the blue table). Return the formula in column I.
You can achieve this by nesting your formulas.
put this in E5
=IF(A5 = "answer", A5 & " " & B5 & " " & D5,
IF(A5 = "bar", A5 & " " & B5 & " " & C5,
IF(A5 = "foo", A5 & " " & B5,
"No If Formula Here yet")))
I started the formula off with the first two formatting's for you.

Resources