VBA Insert Row and total columns based on criteria in column A

JayCS

New Member
Joined
May 6, 2015
Messages
8
OK, I'm need help. I took on a project over my head and I am close to finishing. I just need help on one thing. My project involves Access generating a Cross-Tab query and exporting to Excel. Once it gets to Excel, I need to insert a row and subtotal based on name in column A:

For Example:

NameXXXXXXWeek 1Week 2Week 3Week 4Week 5
Doe, John4050322511
Doe, John1725131023
Smith, Larry3245152214
Smith, Larry111422117

<tbody>
</tbody>

Final product would look like:

NameXXXXXXWeek 1Week 2Week 3Week 4Week 5
Doe, John4050322511
Doe, John1725131023
Subtotal5775453534
Smith, Larry3245152214
Smith, Larry111422117
Subtotal4359373321

<tbody>
</tbody>


I need to insert a row at the end of Doe, John and total the columns that have numbers. Actually, I need to insert a row at the end of every person and add the columns that have numbers. The following week, the spreadsheet will have an extra column "Week 6" and this will go on to infinity...theoretically. Can someone help me with the VBA to insert a subtotal row for each individual and to sum the columns where there are numbers. In my actual spreadsheet, Column "A" is the name and the numbers columns start at column "D" and go out from there.

Any help would be much appreciated.

Thanks,

Jay
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi,

Select your table including the headers then

View tab | Outline group | Subtotal

It's now fairly intuitive bit in the first dropdown select 'Name'
In the second select 'Sum'
Check 'Summary below data' and click OK and you're done.
 
Upvote 0
Hi,

Select your table including the headers then

View tab | Outline group | Subtotal

It's now fairly intuitive bit in the first dropdown select 'Name'
In the second select 'Sum'
Check 'Summary below data' and click OK and you're done.

Thanks for the help. Here is my problem. I have no problem subtotaling myself but I am developing this for someone else and wanted to create a macro that would do this. Every week, they will get a new worksheet that will continue to grow. Currently there are 42 columns that have to be subtotaled. So, to use this method, the end user would have to checkmark 42 fields this week, 43 next week, and so on. I found some VBA online that adds two rows and subtotals column B and C but the script was static. I could manually manipulate the script to include all the columns I need but in 1 weeek, that script will be outdated.

Does anyone have scripting that will accomplish this?

Thanks,

Jay
 
Upvote 0
Try:

Code:
Sub subtotalmacro()
Range("A1:H" & Cells(Rows.Count, 1).End(xlUp).Row).Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4, 5, 6, 7, _
8), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Columns("A:A").EntireColumn.AutoFit
End Sub

on your example above, add more column numbers to the array as you like
 
Upvote 0
It sounds like you should be exporting the query without transform/pivot and then letting Excel do it for you while subtotaling (i.e. a pivot table)
 
Upvote 0
The VBA posted worked but I noticed the range was from Column "D" through Column "H". This will continue to grow every week. Is there any way to change the code to say from Column "D" to the last column that contains numbers?
 
Upvote 0
This?

Code:
Sub subtotalmacro1()
Dim i%, lc%, colarray()
lc = Cells(1, Columns.Count).End(xlToLeft).Column
ReDim colarray(1 To lc - 3)
For i = 4 To lc
colarray(i - 3) = i
Next
Range("A1:" & Cells(Cells(Rows.Count, 1).End(xlUp).Row, lc).Address).Subtotal GroupBy:=1, Function:=xlSum, _
TotalList:=colarray, Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Columns("A:A").EntireColumn.AutoFit
End Sub
 
Last edited:
Upvote 0
Actually you don't need the loop (got this from Rory and Nate)

Code:
Sub subtotalmacro3()
Dim lc%
lc = Cells(1, Columns.Count).End(xlToLeft).Column
Range("A1:" & Cells(Cells(Rows.Count, 1).End(xlUp).Row, lc).Address).Subtotal GroupBy:=1, Function:=xlSum, _
TotalList:=Application.Transpose(Evaluate("INDEX(ROW(4:" & lc & "),,0)")), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Columns("A:A").EntireColumn.AutoFit
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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