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

后端 未结 4 1410
半阙折子戏
半阙折子戏 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:17

    Try somthing like this:

    LastRow = ActiveSheet.UsedRange.Rows.Count
    Set r = ActiveSheet.UsedRange.Resize(1)
    With Application.WorksheetFunction
    
        For iRow = LastRow-1 to 2 step -1
            do while Cells(iRow, 1) = Cells(iRow + 1, 1) 
                LastCol = r(r.Count).Column
                SumCol = LastCol + 1
                   For iCol = 2 To SumCol
                   Cells(iRow, iCol) = .Sum(Range(Cells(iRow, iCol), Cells(iRow + 1, iCol)))
                   Next iCol
                Rows(iRow + 1).Delete
            loop
        Next iRow
    
    End With
    

提交回复
热议问题