Excel Macro - Rows to Comma Separated Cells (Preserve/Aggregate Column)

后端 未结 2 1427
面向向阳花
面向向阳花 2020-12-12 05:39

Based on this Data:

    <- A (Category) ->   <- B (Items) -> 
1   Cat1                 a
2   Cat1                 b
3   Cat1                 c
4          


        
2条回答
  •  死守一世寂寞
    2020-12-12 06:17

    You may try this:

    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
    

提交回复
热议问题