Search code examples
excelstring-concatenation

Add text of cell range in one cell in Excel


I want to concatenate a range of cells into one cell, without macros/vbscript.

The formula CONCATENATE() gets individual cells.


Solution

  • Its not that easy, but I end up with a solution that works wonders!

    A1: the text to search

    B1:BN: The range within the results would go

    B5: The delimiter text

    =MID($A$1,LEN(CONCAT($B$1:B1))+COUNTA($B$1:B1)*LEN($B$5)+1,
        SEARCH(
            $B$5,
            $A$1,
            LEN(
               CONCAT($B$1:B1)) + COUNTA($B$1:B1)*LEN($B$5)+1)
               -(LEN(CONCAT($B$1:B1))+COUNTA($B$1:B1)*LEN($B$5)+1))
    

    As for now it works perfect. Note that you can use whatever text as delimiter. In my case it was "comma + space".

    Where Concat is a simple function that concatenates a range of cells:

    Function Concat(myRange As Range) As String
      Dim r As Range
      Application.Volatile
      For Each r In myRange
        If Len(r.Text) Then
          Concat = Concat & IIf(Concat <> "", "", "") & r.Text
        End If
      Next
    End Function