Add new row if conditions are met then sum row

SoPassive

New Member
Joined
Nov 24, 2016
Messages
4
Hey all,

I am looking to come up with a process that loops through the entire name row and combines the Person's "Total" rows and sums all the columns together.

In the example image below I would like all of Person 1's values to be totaled into 1 row labeled "Person 1 total". Then delete the previous "Total" rows. Then move onto person 2.

05165e6bf7a54b0021813e5b3d7138ee.png


Here is my code below, I seem to have an issue with it forever looping and creating rows, because it runs for at least 10 minutes and then runs out of memory.
Code:
Sub Sum()

    Dim r As Range
    Dim cell As Range
    
    
    Application.ScreenUpdating = False
    
    
    'Set r = Range("B2:B15000") 'ACTUAL RANGE
    Set r = Range("B2:B20") 'EXAMPLE RANGE
    For Each cell In r
        If cell.Value = cell.Offset(-1, 0).Value And cell.Value <> cell.Offset(1, 0).Value Then
            cell.Offset(1).EntireRow.Insert
            cell.Offset(1, 0).Value = cell.Value
        End If
    Next
    
        
    Application.ScreenUpdating = True
    Range("A2").Select
End Sub

I haven't been able to begin trying to sum all of the rows yet, just stuck on creating rows but if any of you could help me out that would be greatly appreciated.

Thanks.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Press F8 to step through the code and you'll find that the code is stuck in an infinite loop. It is because the newly inserted row also meets the IF condition. So, the code just keeps inserting rows.

You can do it from bottom up. Try this:

Code:
Dim i As Integer

    For i = 20 To 2 Step -1    
    If Cells(i, 2).Value <> Cells(i - 1, 2).Value Then
    Cells(i, 2).EntireRow.Insert
    Cells(i, 2).Value = Cells(i - 1, 2).Value
    Cells(i, 3).Value = "newly inserted" '<- this tells which row is the newly inserted row
    End If
    Next i
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,618
Messages
6,120,544
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top