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), "|", ", ")
Related
I have a table in Excel that has 10 columns. For each row there are different numbers of cells populated e.g.
My concatenated string is as follows for HTML emails
(" + Machine1 + ", " + Machine2 + ", " + Machine3 + ", " + Machine4 + ", " + Machine5 + ", " + Machine6 + ")
However I want the comma to not show after the last cell in the row (whether that be column 3,4,5,6).
This is what I get
(TEST1, TEST2, TEST3 , TEST4, TEST5, , )
I want to remove the two commas at the end. Hope this makes sense!
If you have Office 365 Excel then use TEXTJOIN:
="(" & TEXTJOIN(", ",TRUE,A2:F2) & ")"
If not then you will need to use IF for each return and Mid:
=MID("(" & IF(A2 <> "",", " & A2,"") & IF(A2 <> "",", " & B2,"") & IF(C2 <> "",", " & C2,"") & IF(D2 <> "",", " & D2,"") & IF(E2 <> "",", " & E2,"") & IF(F2 <> "",", " & F2,""),3,999)
Alternative without IF
Just in addition to Scott's valid solution, I demonstrate an approach via the REPT function. Applied on a cell containing a string, it "repeats" its content & comma once (indicated by COUNTA() equalling 1), whereas an empty cell results in a zero repetition which allows to omit not only the cell content but the comma, too:
="(" & SUBSTITUTE(REPT(A1&",",COUNTA(A1))&REPT(B1&",",COUNTA(B1))&REPT(C1&",",COUNTA(C1))&REPT(D1&",",COUNTA(D1))&REPT(E1&",",COUNTA(E1))&REPT(F1&",",COUNTA(F1))&"$",",$",")")
A simple SUBSTITUTE removes the last comma, wherever it occurs before the closing bracket ")".
Cell A2 contains value "25-86".
I need cell B2 to have value "AJ 2586%"
This code reveals Syntax error:
Range("B2").Value = "AJ & Left(Range("A2"), 2) & Right(Range("A2"), 2) & %"
If I write it like that
Range("B2").Value = "AJ & Left(A2, 2) & Right(A2, 2) & %"
The functions Left and Right treat "A2" as string.
How could I extract a part of text from the cell and enter it in another cell?
The issue is that everything between the " " defines a string.
You can type your code like this:
Range("B2").Value = "AJ " & Left(Range("A2"), 2) & Right(Range("A2"), 2) & "%"
The following formula will return the value as a real percentage that you can use in calculations.
It just removes the - from the figure.
=SUBSTITUTE(A2,"-","")/100
Initially it will display the value as 25.86.
When you apply the custom number format of:
\AJ 0%
It will then display as AJ 2586%.
If that formula is in A1 then =A1/2 will return AJ 1293%.
Edit:
Just realised you want it in VBA:
Range("B2") = Replace(Range("A2"), "-", "") / 100
Range("B2").NumberFormat = "\AJ 0%"
Or, if you just want it as text:
Range("B2") = "AJ " & Replace(Range("A2"), "-", "") & "%"
Range("B2").Value = "AJ " + Left(Range("A2"), 2) + Right(Range("A2"), 2) + "%"
Indent code 4 spaces in order for it to be in the correct format
I'm trying to combine contents from multiple cells in excel. Here is an example of what i'm wanting to do:
(Cell1): John Doe
(Cell2): 5950
(Cell3): Autofill with "John Doe" XXXXXX5950
I know i can combine the cells using the formula =&[Cell1]&" XXXXXX"&[Cell2]
But I want it to put the " " on each side of the name, but the code won't except it since part of using the formula is using the " symbol. Any help on this?
Use two double-quotes inside a string to put a double-quote:
=""""&[Cell1]&""" XXXXXX"&[Cell2]
You can escape the " character in VBA by using double quotes.
Range("A1").Formula = """" & Range("B1").Value & """ XXXXXX" & Range("C1").Value
Or you can also use ASCII character 34 as well.
Range("A1").Formula = Chr(34) & Range("B1").Value & Chr(34) & " XXXXX" & Range("C2").Value
I have the following sample data,
I want to concatenate string1 and string2 to produce the concatenation result
"a" "b"
using the concatenate function
I did not find any the answer in official microsoft excel documentation
https://support.office.com/en-us/article/CONCATENATE-function-8f8ae884-2ca8-4f7a-b093-75d702bea31d
How would I do this in excel? Concatenate " quotation marks
I know it's not the prettiest solution but:
=CONCATENATE(CHAR(34) & A1 & CHAR(34), " " & CHAR(34) & B1 & CHAR(34))
or
=CONCATENATE(CHAR(34), A1, CHAR(34), " ", CHAR(34), B1, CHAR(34))
CHAR(34) can be used in place of ".
Edit:
If you prefer, CHAR(34) can be replaced with """", four double quote marks - for the same effect.
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") & ")"