VBA - Insert blank row and sum values in column C for each value in column D

jmk1153

New Member
Joined
Jun 27, 2018
Messages
14
Hello,

I think what I'm trying to accomplish is relatively easy but I am really struggling on this one...

One of the problems is I will need to do this everyday with different data so I'm trying to use a formula that can be used for all data in this same format.

I have a sheet with 4 columns. What I'm trying to do is, insert two new blank rows after each change in value in column D (e.g. two new rows between A36 and A37, as well as A81 and A82). The first new row would contains the sum of values in column C for that group.

So a new row would be inserted as 37 and 38. Cell C37 would contain the value 18,041. Row 38 would remain blank. The new row inserted for row 68 would contain a value of -10,490 in C68, etc. Row 69 would remain blank.

Same deal for the Hyundia group.

Is this way more complicated than I think?

PersonCarSalesDealership
JohnAccord4500Honda
JohnAccord4600Honda
JohnAccord4600Honda
JohnAccord2500Honda
JohnAccord520Honda
JohnAccord178Honda
JohnAccord154Honda
JohnAccord148Honda
JohnAccord115Honda
JohnAccord97Honda
JohnAccord67Honda
JohnAccord63Honda
JohnAccord60Honda
JohnAccord51Honda
JohnAccord40Honda
SheilaS200038Honda
SheilaS200035Honda
SheilaS200034Honda
SheilaS200026Honda
SheilaS200025Honda
SheilaS200025Honda
SheilaS200024Honda
SheilaS200023Honda
SheilaS200021Honda
SheilaS200019Honda
SheilaS200016Honda
SheilaS200015Honda
SheilaS200011Honda
SheilaS20007Honda
SheilaS20007Honda
SheilaS20007Honda
SheilaS20006Honda
SheilaS20003Honda
SheilaS20003Honda
SheilaS20003Honda
EdwardOutback3Subaru
EdwardOutback2Subaru
EdwardOutback2Subaru
EdwardOutback2Subaru
EdwardOutback1Subaru
EdwardOutback1Subaru
EdwardOutback1Subaru
EdwardOutback1Subaru
EdwardOutback-1524Subaru
EdwardOutback-1083Subaru
EdwardOutback-834Subaru
EdwardOutback-600Subaru
EdwardOutback-208Subaru
EdwardOutback-94Subaru
EdwardOutback-89Subaru
EdwardOutback-83Subaru
EdwardImpreza-82Subaru
EdwardImpreza-81Subaru
EdwardImpreza-1524Subaru
EdwardImpreza-1083Subaru
EdwardImpreza-834Subaru
EdwardImpreza-600Subaru
EdwardImpreza-208Subaru
EdwardImpreza-94Subaru
EdwardImpreza-89Subaru
EdwardImpreza-83Subaru
EdwardImpreza-82Subaru
EdwardImpreza-81Subaru
EdwardImpreza-208Subaru
EdwardImpreza-94Subaru
EdwardImpreza-89Subaru
JeffLegacy-83Subaru
JeffLegacy-82Subaru
JeffLegacy-81Subaru
JeffLegacy-80Subaru
JeffLegacy-80Subaru
JeffLegacy-73Subaru
JeffLegacy-59Subaru
JeffLegacy-47Subaru
JeffLegacy-37Subaru
JeffLegacy-30Subaru
JeffLegacy-28Subaru
JeffLegacy-23Subaru
JeffLegacy-22Subaru
JeffLegacy-18Subaru
SveaSanta Fe4500Hyundai
SveaSanta Fe3200Hyundai
SveaSanta Fe4600Hyundai
SveaSanta Fe11Hyundai
SveaSanta Fe9Hyundai
SveaSanta Fe9Hyundai
SveaSanta Fe8Hyundai
SveaSanta Fe7Hyundai
SveaSanta Fe7Hyundai
SveaSanta Fe7Hyundai
SveaSanta Fe6Hyundai
SveaSanta Fe5Hyundai
SveaSanta Fe5Hyundai
SveaSanta Fe5Hyundai

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this

Code:
Sub Insert_blank_row()
    Dim i As Long, b As Range
    For i = Range("D" & Rows.Count).End(xlUp).Row To 2 Step -1
        dealer = Cells(i, "D").Value
        Set b = Range("D:D").Find(dealer, LookIn:=xlValues, lookat:=xlWhole)
        If Not b Is Nothing Then
            Rows(i + 1 & ":" & i + 2).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            Cells(i + 1, "C").Value = WorksheetFunction.Sum(Range("C" & b.Row, "C" & i))
            i = b.Row
        End If
    Next
End Sub

You can also put subtotals with a Pivot table or with the subtotals excel functionality. eg.

Subtotals---Figure-1.png
 
Last edited:
Upvote 0
Thank you!

That works well. That is much simpler than what I was trying. Appreciate the help.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,731
Members
449,093
Latest member
Mnur

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