Excel: Concatenate Cells and Remove Duplicates

╄→尐↘猪︶ㄣ 提交于 2019-12-11 16:42:29

问题


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?


回答1:


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.


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


来源:https://stackoverflow.com/questions/47535683/excel-concatenate-cells-and-remove-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!