Insert Row VBA

rusted314

Board Regular
Joined
Jan 12, 2010
Messages
74
I have report that I am adding subtotals to using the Excel 2010 subtotal feature. I now need to add a row before and after each subtotal which is column E in my document. Below is an example. Does anyone have a simple macro that will do this?

100</SPAN>
111</SPAN>
2/8/2013</SPAN>
100 Total</SPAN>
105</SPAN>
100</SPAN>
2/28/2013</SPAN>
105</SPAN>
313</SPAN>
2/7/2013</SPAN>

<TBODY>
</TBODY>
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
If you do that you'll lose the Sub-Total functionality unless you remove those rows.
 
Upvote 0
What I mean is that if you add rows like that then the sheet will be seen as a series of different data ranges, and no longer contiguous. The Sub-Total function needs a contiguous data range.
 
Upvote 0
With a sample Table like so...

Excel 2010
ABCD
1DeptCode Revenue Select
2701895 52.00 y
3701756 75.00 n
4701325 82.00 n
5701111 - n
6702222 15.00 n
770 Total 224.00
8711895 16.00 y
9711777 17.00 n
10711756 18.00 y
11711325 19.00 n
12711111 20.00 n
13712222 21.00 y
14711998 22.00 n
1571 Total 133.00
161001895 23.00 y
171001777 24.00 y
181001756 25.00 n
191001325 26.00 n
201001111 27.00 y
211002222 28.00 n
221001998 29.00 y
23100 Total 182.00
24Grand Total 539.00

<colgroup><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



Run this code against...

Code:
Sub Foo()
With Application
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With
LR = Range("A" & Rows.Count).End(xlUp).Row - 1
For i = LR To 1 Step -1
    If InStr(1, Cells(i, 1), "Total", vbTextCompare) Then
        Cells(i, 1).Offset(1).EntireRow.Insert
        Cells(i, 1).EntireRow.Insert
    End If
Next i
With Application
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With
End Sub
 
Upvote 0
Unfortunately the VBA didnt work but I appreciate you sharing it with me. Yes, my table is setup very similiar to yours example except my totals are in column E and I am sub totaling columns H and I. To Smittys point maybe this cant be done using the subtotal feature or maybe i need a macro to do the subtotals for me?
 
Upvote 0
I ended up finding a macro online that I was able to modify that did the trick. Thank you all for your help.

Sub InsertRows()
Dim r As Long
Dim m As Long
Application.ScreenUpdating = False
m = Range("E" & Rows.Count).End(xlUp).Row
For r = m To 1 Step -1
If Range("E" & r) Like "*Total" Then
Range("E" & (r + 1)).EntireRow.Insert

End If

If Range("E" & r) Like "*Total" Then
Range("E" & r).EntireRow.Insert

End If

Next r
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,617
Members
449,039
Latest member
Mbone Mathonsi

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