If you want to take text from multiple columns and merge it into one, you can easily do it in Excel.
There are a few ways to do it, using an ampersand (&), the CONCAT function, or VBA.
In this lesson, I’m going to show you all these methods.
First, let’s take a look at the example.
“This is plain text” the message says. We want to take all these cells from different columns and merge them into one cell.
Columns to text with an ampersand
Let’s start with an ampersand. Enter the following formula into a cell.
=A1&" "&B1&" "&C1&" "&D1&E1
This is how it looks like.
Columns to text with a function
The CONCAT function (formerly CONCATENATE) will merge multiple cells into a single one.
You can select all cells at once, but there will be no separation between values.
This formula will return this text.
For this reason, you have to add separation between each word.
=CONCAT(A1," ",B1," ",C1," ",D1,E1)
And now, we have text separated by spaces.
Columns to text with VBA
This one is going to make our work much easier. But first, we have to create a VBA function.
Press Left Alt + F11 to open VBA for application.
Insert a new module and enter this function.
Public Function ColumnsToText(rng As Range, Optional delimiter As String = " ") As String Dim var As Variant var = Application.Transpose(Application.Transpose(rng.Value)) ColumnsToText = Join(var, delimiter) End Function
Let’s use this function. Enter a range as an only argument.
And the effect will be similar to the previous examples.
"This is plain text ."
You can also add a delimiter to change the default one.
The problem with this function is that there is no way to remove the delimiter before a dot.
Let’s modify this function by adding the third optional parameter and conditional statements.
Public Function ColumnsToText(rng As Range, Optional delimiter As String = " ", Optional pMark As Boolean = False) As String Dim var As Variant var = Application.Transpose(Application.Transpose(rng.Value)) replaceStr = Join(var, delimiter) If pMark = True Then ColumnsToText = StrReverse(Replace(StrReverse(replaceStr), StrReverse(delimiter), StrReverse(""), , 1)) Else ColumnsToText = replaceStr End If End Function
The third argument, which is by default FALSE will indicate whether there is a punctuation mark at the end, if there is, the last delimiter is removed.
This is how to execute this function.
=ColumnsToText(A1:E1) =ColumnsToText(A1:E1, " ") =ColumnsToText(A1:E1, " ", FALSE)
All of them return the same result.
"This is plain text ."
Let’s try the next one.
=ColumnsToText(A1:E1, " ", TRUE)
This one returns the result we want.
"This is plain text."