Excel:连接单元格并删除重复项 [英] Excel: Concatenate Cells and Remove Duplicates

查看:136
本文介绍了Excel:连接单元格并删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何连接单元格B1:K1中的值以在A1中接收字符串.空单元格应省略.使用Excel命令或仅使用vba可以做到这一点吗?

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?

推荐答案

如果您具有Office 365 Excel,则可以使用TEXTJOIN的数组形式:

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}))))))

作为数组,退出编辑模式时需要使用Ctrl-Shift-Enter而不是Enter进行确认.

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

如果您没有Office 365 Excel,则需要vba.此UDF将模仿TEXTJOIN.将其放在工作簿附带的模块中,并使用如上所述的公式.

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

这篇关于Excel:连接单元格并删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆