基于重复值连接单元格值?

时间:2017-06-23 15:51:43

标签: excel vba excel-vba

基本上我的数据(项目编号及其相关尺寸)在两列中是这样的:

.seek(pos)

如果列A具有重复值,我需要连接B列中的维度(使用分隔每个大小的换行符)。所以出乎意料的是:

FOX6215A     - Twin Dimensions (W x D x H): 38.6" x 1.3" x 59.8"
FOX6215A     - Full Dimensions (W x D x H): 53.6" x 1.3" x 59.8"
FOX6215A     - Queen Dimensions (W x D x H): 60.6" x 1.3" x 59.8"
FOX6215A     - King Dimensions (W x D x H): 76.6" x 1.3" x 59.8"
FOX6215B     - Twin Dimensions (W x D x H): 38.6" x 1.3" x 59.8"
FOX6215B     - Full Dimensions (W x D x H): 53.6" x 1.3" x 59.8"
FOX6215B     - Queen Dimensions (W x D x H): 60.6" x 1.3" x 59.8"
FOX6215B     - King Dimensions (W x D x H): 76.6" x 1.3" x 59.8"
FOX6215C     - Twin Dimensions (W x D x H): 38.6" x 1.3" x 59.8"
FOX6215C     - Full Dimensions (W x D x H): 53.6" x 1.3" x 59.8"

因为 FOX6215A 有多种尺寸。我完全不知道如何做到这一点。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果您有Office 365 Excel with TEXTJOIN():

获取项目编号的唯一列表,然后使用此数组公式:

=TEXTJOIN(CHAR(10),TRUE,IF($A$1:$A$10=D1,$B$1:$B$10,""))

作为数组公式,需要在退出编辑模式时使用Ctrl-Shift-Enter而不是Enter来确认。如果操作正确,那么Excel会将{}放在公式周围。

请记住在输出单元格上启用Wrap Text并相应地调整大小。

enter image description here

如果您没有Office 365 Excel:

将此代码放在工作簿附带的模块中,并使用上述公式:

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