Excel宏-行到逗号分隔的单元格(保留/聚合列)

Excel宏-行到逗号分隔的单元格(保留/聚合列),excel,vba,Excel,Vba,根据这些数据: <- A (Category) -> <- B (Items) -> 1 Cat1 a 2 Cat1 b 3 Cat1 c 4 Cat2 d 5 Cat3 e 6 Cat4 f 7 Cat4 g

根据这些数据:

    <- A (Category) ->   <- B (Items) -> 
1   Cat1                 a
2   Cat1                 b
3   Cat1                 c
4   Cat2                 d
5   Cat3                 e
6   Cat4                 f
7   Cat4                 g

1第1类a
2第1类b
3第1类c
4第2类d
5第3类e
6 Cat4 f
7 Cat4 g
我需要这个:

    <- A (Category) ->   <- B (Items) -> 
1   Cat1                 a,b, c
2   Cat2                 d
3   Cat3                 e
4   Cat4                 f, g

1第1类a、b、c
2类2 d
3第3类e
4第4类f、g
您可以尝试以下方法:

Sub GroupMyValues()

    Dim oCell As Excel.Range
    Dim sKey As String
    Dim sResult As String

    Set oCell = Worksheets(2).Range("A1")

    While Len(oCell.Value) > 0

        If oCell.Value <> sKey Then

            'If first entry, no rows to be deleted
            If sKey <> "" Then

                oCell.Offset(-1, 1).Value = sResult

            End If

            sKey = oCell.Value
            sResult = oCell.Offset(0, 1).Value
            Set oCell = oCell.Offset(1, 0)

        Else

            sResult = sResult & ", " & oCell.Offset(0, 1).Value

            Set oCell = oCell.Offset(1, 0)
            oCell.Offset(-1, 0).EntireRow.Delete

        End If

    Wend

    'Last iteration
    oCell.Offset(-1, 1).Value = sResult

End Sub
子组MyValues()
将oCell设置为Excel.Range
像线一样模糊
作为字符串的Dim sResult
设置oCell=工作表(2)。范围(“A1”)
而Len(oCell.Value)>0
如果oCell.Value sKey那么
'如果是第一个条目,则没有要删除的行
如果是“sKey”,那么
oCell.Offset(-1,1)。值=sResult
如果结束
sKey=oCell.Value
sResult=oCell.Offset(0,1).Value
设置oCell=oCell.Offset(1,0)
其他的
sResult=sResult&“,”和oCell.Offset(0,1).Value
设置oCell=oCell.Offset(1,0)
oCell.Offset(-1,0).EntireRow.Delete
如果结束
温德
“最后一次迭代
oCell.Offset(-1,1)。值=sResult
端接头
您可以尝试以下方法:

Sub GroupMyValues()

    Dim oCell As Excel.Range
    Dim sKey As String
    Dim sResult As String

    Set oCell = Worksheets(2).Range("A1")

    While Len(oCell.Value) > 0

        If oCell.Value <> sKey Then

            'If first entry, no rows to be deleted
            If sKey <> "" Then

                oCell.Offset(-1, 1).Value = sResult

            End If

            sKey = oCell.Value
            sResult = oCell.Offset(0, 1).Value
            Set oCell = oCell.Offset(1, 0)

        Else

            sResult = sResult & ", " & oCell.Offset(0, 1).Value

            Set oCell = oCell.Offset(1, 0)
            oCell.Offset(-1, 0).EntireRow.Delete

        End If

    Wend

    'Last iteration
    oCell.Offset(-1, 1).Value = sResult

End Sub
子组MyValues()
将oCell设置为Excel.Range
像线一样模糊
作为字符串的Dim sResult
设置oCell=工作表(2)。范围(“A1”)
而Len(oCell.Value)>0
如果oCell.Value sKey那么
'如果是第一个条目,则没有要删除的行
如果是“sKey”,那么
oCell.Offset(-1,1)。值=sResult
如果结束
sKey=oCell.Value
sResult=oCell.Offset(0,1).Value
设置oCell=oCell.Offset(1,0)
其他的
sResult=sResult&“,”和oCell.Offset(0,1).Value
设置oCell=oCell.Offset(1,0)
oCell.Offset(-1,0).EntireRow.Delete
如果结束
温德
“最后一次迭代
oCell.Offset(-1,1)。值=sResult
端接头

如果您想保留原始数据,而只是在其他地方汇总数据,可以使用以下方法

在VB中创建一个用户定义的函数,该函数本质上类似于CONCATENATE,但可以在数组公式中使用。这将允许您在连接函数中为范围变量插入IF语句。下面是我拼凑的一个快速版本:

Private Function CCARRAY(rr As Variant, sep As String)
'rr is the range or array of values you want to concatenate.  sep is the delimiter.
Dim rra() As Variant
Dim out As String
Dim i As Integer

On Error GoTo EH
rra = rr

out = ""
i = 1

Do While i <= UBound(rra, 1)
    If rra(i, 1) <> False Then
        out = out & rra(i, 1) & sep
    End If
    i = i + 1
Loop
out = Left(out, Len(out) - Len(sep))

CCARRAY = out
Exit Function

EH:
rra = rr.Value
Resume Next

End Function

请记住在输入公式时按Ctrl+Shift+Enter。

如果您想保留原始数据并仅在其他地方汇总数据,可以使用以下方法

在VB中创建一个用户定义的函数,该函数本质上类似于CONCATENATE,但可以在数组公式中使用。这将允许您在连接函数中为范围变量插入IF语句。下面是我拼凑的一个快速版本:

Private Function CCARRAY(rr As Variant, sep As String)
'rr is the range or array of values you want to concatenate.  sep is the delimiter.
Dim rra() As Variant
Dim out As String
Dim i As Integer

On Error GoTo EH
rra = rr

out = ""
i = 1

Do While i <= UBound(rra, 1)
    If rra(i, 1) <> False Then
        out = out & rra(i, 1) & sep
    End If
    i = i + 1
Loop
out = Left(out, Len(out) - Len(sep))

CCARRAY = out
Exit Function

EH:
rra = rr.Value
Resume Next

End Function
输入公式时,请记住按Ctrl+Shift+Enter