Don’t you find it hard that for combining text from multiple cells, you have to select individual cells rather than selecting a range? One can easily create a user-defined function in Excel to add this functionality and make it easier to concatenate with a delimiter.
Below, I present a user-defined function (UDF) that you can add to an Excel add-in.
With this UDF, it’s possible to concatenate an unlimited number of ranges using a ParamArray. The delimiter is a required parameter.
Example usage: =CONCATRANGES(";",A1:A3,B5,C6:D7,"X",1,{"A","B","C"})
The code for the function: CONCATRANGES()
Public Function CONCATRANGES(ByVal sDelim As String, ParamArray rRanges()) As String Dim rRange, rCell As Excel.Range, aVal() As String, i As Long, iSize As Long, sType As String, vCell As Variant iSize = 0 i = 1 For Each rRange In rRanges sType = TypeName(rRange) Select Case sType Case "Range" iSize = iSize + rRange.Count ReDim Preserve aVal(1 To iSize) For Each rCell In rRange aVal(i) = rCell.Text i = i + 1 Next Case "Variant()" iSize = iSize + UBound(rRange) ReDim Preserve aVal(1 To iSize) For Each vCell In rRange aVal(i) = vCell i = i + 1 Next Case Else iSize = iSize + 1 ReDim Preserve aVal(1 To iSize) aVal(i) = rRange i = i + 1 End Select Next CONCATRANGES = Join(aVal, sDelim) End Function
Let's explain what's in the function. rRanges
is a ParamArray, and this needs to be iterated in the function to find all ranges referenced by the user. There are several types of values and ranges that may be referenced in a function. Normally, users will only use actual cell references in their formulas. But some users may decide to have a regular value (e.g. "X"
or 1.2
) in the formula. In an advanced scenario, one can even use an array of values (e.g. {"A","B","C"}
).
Therefore, the function looks at the type of a cell, which can be Range
(regular cell reference), Variant()
(array of values) or other (regular value). Each needs to be treated in a different way.
I hope you will find this function useful. Please let me know how you are using it!