Macro to insert new row when value changes with data from the cell above & autosum

Mux99

Board Regular
Joined
Apr 15, 2019
Messages
57
Hi all

I would like to insert a new row after the value changes in column C and in the new row copy the value from the cell above in column C and display the total in column D.

98ecd3.jpg


After running the macro it should look like the below

1oopvn.jpg


Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi & welcome to MrExcel
How about
Code:
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
 
Last edited:
Upvote 0
Hi & welcome to MrExcel
How about
Code:
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

Thanks for your help. This added a new row and copied the data from row C but the total for row D didn't work. Also is it possible to insert a new row like the rest on the last row with data in it.

Run-time error 1004.
No Cells were found.
 
Upvote 0
What do you have in column D?
 
Upvote 0
Ok, how about
Code:
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
 
Upvote 0
Ok, how about
Code:
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

This one works perfectly. Thanks for the help
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

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