![]() |
Line insertion
Hi,
Can anyone tell me how I use VBA to compare rows to each other and if the previous row is different to the current row then insert a line and sum the total of the row that are equal? This is really driving me mad. I have tried using the Sumtotals option in the data menu in excel but I want to compare rows using two or more cells in a row. ie coparing by column b then by coulmn a and summing column c. Can this be done is VBA? Any help would be great thanks Andy |
Line insertion
Sub Tester2()
Dim Start As Range, Cell As Range Set Start = Range("C1") Set Cell = Range("B2") Do While Not IsEmpty(Cell) If Cell.Value < Cell(0).Value Or _ Cell(1, 0).Value < Cell(0, 0).Value Then Cell.EntireRow.Insert Cell(0, 2).Formula = "=sum(" & _ Range(Start, Cell(-1, 2)).Address & ")" Set Start = Cell(1, 2) End If Set Cell = Cell(2, 1) Loop Cell(1, 2).Formula = "=sum(" & _ Range(Start, Cell(0, 2)).Address & ")" End Sub -- Regards, Tom Ogilvy "Andy" wrote in message ... Hi, Can anyone tell me how I use VBA to compare rows to each other and if the previous row is different to the current row then insert a line and sum the total of the row that are equal? This is really driving me mad. I have tried using the Sumtotals option in the data menu in excel but I want to compare rows using two or more cells in a row. ie coparing by column b then by coulmn a and summing column c. Can this be done is VBA? Any help would be great thanks Andy |
Line insertion
Thanks for the help. -----Original Message----- Sub Tester2() Dim Start As Range, Cell As Range Set Start = Range("C1") Set Cell = Range("B2") Do While Not IsEmpty(Cell) If Cell.Value < Cell(0).Value Or _ Cell(1, 0).Value < Cell(0, 0).Value Then Cell.EntireRow.Insert Cell(0, 2).Formula = "=sum(" & _ Range(Start, Cell(-1, 2)).Address & ")" Set Start = Cell(1, 2) End If Set Cell = Cell(2, 1) Loop Cell(1, 2).Formula = "=sum(" & _ Range(Start, Cell(0, 2)).Address & ")" End Sub -- Regards, Tom Ogilvy "Andy" wrote in message ... Hi, Can anyone tell me how I use VBA to compare rows to each other and if the previous row is different to the current row then insert a line and sum the total of the row that are equal? This is really driving me mad. I have tried using the Sumtotals option in the data menu in excel but I want to compare rows using two or more cells in a row. ie coparing by column b then by coulmn a and summing column c. Can this be done is VBA? Any help would be great thanks Andy . |
All times are GMT +1. The time now is 10:28 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com