How to SUM / merge similar rows in Excel using VBA?

后端 未结 4 1418
半阙折子戏
半阙折子戏 2020-12-10 09:30

I\'m trying to build a simple macro in VBA for Excel that would SUM [merge] all the rows that have the same name (value in the first columns). So for example



        
4条回答
  •  一个人的身影
    2020-12-10 10:08

    This code is easier to read and does the job:

    Sub Macro1()
        Dim ColumnsCount As Integer
    
        ColumnsCount = ActiveSheet.UsedRange.Columns.Count
    
        ActiveSheet.UsedRange.Activate
    
    
        Do While ActiveCell.Row <= ActiveSheet.UsedRange.Rows.Count
            If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then
                For i = 1 To ColumnsCount - 1
                    ActiveCell.Offset(0, i).Value = ActiveCell.Offset(0, i).Value + ActiveCell.Offset(1, i).Value
                Next
                ActiveCell.Offset(1, 0).EntireRow.Delete shift:=xlShiftUp
            Else
                ActiveCell.Offset(1, 0).Select
            End If
        Loop
    
    
    End Sub
    

提交回复
热议问题