Sub Mux99()
Dim i As Long
Dim Rng As Range
For i = 9 To 2 Step -1
If Cells(i, 3) <> Cells(i - 1, 3) Then
Rows(i).Insert
Cells(i, 3) = Cells(i - 1, 3)
End If
Next i
For Each Rng In Range("D:D").SpecialCells(xlConstants).Areas
Rng.Offset(Rng.Count).Resize(1).Formula = "=sum(" & Rng.Address & ")"
Next Rng
End Sub
Hi & welcome to MrExcel
How aboutCode:Sub Mux99() Dim i As Long Dim Rng As Range For i = 9 To 2 Step -1 If Cells(i, 3) <> Cells(i - 1, 3) Then Rows(i).Insert Cells(i, 3) = Cells(i - 1, 3) End If Next i For Each Rng In Range("D:D").SpecialCells(xlConstants).Areas Rng.Offset(Rng.Count).Resize(1).Formula = "=sum(" & Rng.Address & ")" Next Rng End Sub
Run-time error 1004.
No Cells were found.
What do you have in column D?
=(B1-A1)
=(B2-A2)
=(B3-A3)
Sub Mux99()
Dim i As Long
Dim Rng As Range
For i = Range("C" & Rows.Count).End(xlUp).Row To 2 Step -1
If Cells(i, 3) <> Cells(i - 1, 3) Then Rows(i).Insert
Next i
For Each Rng In Range("D:D").SpecialCells(xlFormulas).Areas
With Rng.Offset(Rng.Count)
.Resize(1).Formula = "=sum(" & Rng.Address & ")"
.Offset(, -1).Resize(1).Value = .Offset(-1, -1).Value
End With
Next Rng
End Sub
Ok, how aboutCode:Sub Mux99() Dim i As Long Dim Rng As Range For i = Range("C" & Rows.Count).End(xlUp).Row To 2 Step -1 If Cells(i, 3) <> Cells(i - 1, 3) Then Rows(i).Insert Next i For Each Rng In Range("D:D").SpecialCells(xlFormulas).Areas With Rng.Offset(Rng.Count) .Resize(1).Formula = "=sum(" & Rng.Address & ")" .Offset(, -1).Resize(1).Value = .Offset(-1, -1).Value End With Next Rng End Sub