# Add new row if conditions are met then sum row

#### SoPassive

##### New Member
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.

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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

#### yky

##### Well-known Member
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:

Replies
9
Views
760
Replies
1
Views
319
Replies
25
Views
2K
Replies
3
Views
698
Replies
3
Views
467

1,191,097
Messages
5,984,654
Members
439,898
Latest member
Zack0611

### 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.

### Which adblocker are you using?

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

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