Combine value in cells with a comma
This tutorial shows how to combine values in separate cells with a comma (,) through the use of an Excel formula, with the SUBSTITUTE and TRIM functions or VBA
=SUBSTITUTE(TRIM(B5&" "&C5&" "&D5&" "&E5)," ", ", ")
=SUBSTITUTE(TRIM(B5&" "&C5&" "&D5&" "&E5)," ",F5&" ")
|
GENERIC FORMULA
=SUBSTITUTE(TRIM(value1&" "&value2&" "&...)," ", ", ")
ARGUMENTS GENERIC FORMULA
=SUBSTITUTE(TRIM(value1&" "&value2&" "&...)," ",comma&" ")
ARGUMENTS EXPLANATION This formula uses the SUBSTITUTE and TRIM functions with the & sign to combine values captured in separate cell with a comma.
Click on either the Hard Coded or Cell Reference button to view the formula that either has the comma directly entered in the formula or referenced to a cell that captures the comma sign that is to be inserted between the values that you want to combine.
In this example we are combining the values captured in B5, C5, D5 and E5 with a comma between them. |
Dim ws As Worksheet
ws.Range("F5") = WorksheetFunction.Substitute(WorksheetFunction.Trim(ws.Range("B5") & " " & ws.Range("C5") & " " & ws.Range("D5") & " " & ws.Range("E5")), " ", ", ")
End Sub
Dim ws As Worksheet
ws.Range("G5") = WorksheetFunction.Substitute(WorksheetFunction.Trim(ws.Range("B5") & " " & ws.Range("C5") & " " & ws.Range("D5") & " " & ws.Range("E5")), " ", ws.Range("F5") & " ")
End Sub
Dim ws As Worksheet
'combine values in separate cells with a comma
ws.Range("F" & x) = WorksheetFunction.Substitute(WorksheetFunction.Trim(ws.Range("B" & x) & " " & ws.Range("C" & x) & " " & ws.Range("D" & x) & " " & ws.Range("E" & x)), " ", ", ")
Next
End Sub
Dim ws As Worksheet
'combine values in separate cells with a comma
ws.Range("G" & x) = WorksheetFunction.Substitute(WorksheetFunction.Trim(ws.Range("B" & x) & " " & ws.Range("C" & x) & " " & ws.Range("D" & x) & " " & ws.Range("E" & x)), " ", ws.Range("F5") & " ")
Next
End Sub
Related Topic | Description | Related Topic and Description |
---|---|---|
Insert a comma before first number | How to insert a comma (,) before the first number in a cell through the use of an Excel formula or VBA | |
Insert comma to end of a cell | How to add a comma (,) to the end of a cell through the use of an Excel formula or VBA | |
Insert a comma after first number | How to insert a comma (,) after the first number in a cell through the use of an Excel formula or VBA | |
Insert a comma before each word | How to insert a comma before each word through the use of an Excel formula or VBA |
Related Functions | Description | Related Functions and Description |
---|---|---|
SUBSTITUTE Function | TThe Excel SUBSTITUTE function replaces characters with another in a specified string | |
TRIM Function | The Excel TRIM function removes the spaces at the start and end of the text and also removes the unnecessary spaces between words, leaving only a single space between words and numbers that form part of the text |