Excel: Concatenate Cells and Remove Duplicates

2019-08-21 02:30发布

how can I concatenate the Values in Cells B1:K1 to receive the string in A1. Empty cells should be omitted. Is this possible using Excel commands or only with vba?

enter image description here

1条回答
Juvenile、少年°
2楼-- · 2019-08-21 03:15

If you have Office 365 Excel then you can use an Array form of TEXTJOIN:

=TEXTJOIN(", ",TRUE,INDEX(1:1,,N(IF({1},MODE.MULT(IF((IFERROR(MATCH(B1:K1,B1:K1,0)=COLUMN(B1:K1)-MIN(COLUMN(B1:K1))+1,0))*(B1:K1<>""),COLUMN(B1:K1)*{1;1}))))))

Being an array it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.

enter image description here


If you do not have office 365 Excel you will need vba. This UDF will mimic the TEXTJOIN. Put it in a module attached to the workbook and use the formula as described above.

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
    Dim d As Long
    Dim c As Long
    Dim arr2()
    Dim t As Long, y As Long
    t = -1
    y = -1
    If TypeName(arr) = "Range" Then
        arr2 = arr.Value
    Else
        arr2 = arr
    End If
    On Error Resume Next
    t = UBound(arr2, 2)
    y = UBound(arr2, 1)
    On Error GoTo 0

    If t >= 0 And y >= 0 Then
        For c = LBound(arr2, 1) To UBound(arr2, 1)
            For d = LBound(arr2, 1) To UBound(arr2, 2)
                If arr2(c, d) <> "" Or Not skipblank Then
                    TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
                End If
            Next d
        Next c
    Else
        For c = LBound(arr2) To UBound(arr2)
            If arr2(c) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c) & delim
            End If
        Next c
    End If
    TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
End Function
查看更多
登录 后发表回答