简体   繁体   English

Excel:连接单元格并删除重复项

[英]Excel: Concatenate Cells and Remove Duplicates

how can I concatenate the Values in Cells B1:K1 to receive the string in A1 . 如何连接单元格B1:K1的值以接收A1的字符串。 Empty cells should be omitted. 空单元格应省略。 Is this possible using Excel commands or only with vba? 使用Excel命令或仅使用vba可以实现吗?

在此处输入图片说明

If you have Office 365 Excel then you can use an Array form of TEXTJOIN: 如果您具有Office 365 Excel,则可以使用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. 作为数组,退出编辑模式时,需要使用Ctrl-Shift-Enter而不是Enter进行确认。

在此处输入图片说明


If you do not have office 365 Excel you will need vba. 如果没有Office 365 Excel,则需要vba。 This UDF will mimic the TEXTJOIN. 此UDF将模仿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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM