Avatar of llawrenceg
llawrencegFlag for United States of America

asked on 

Caculating subtotals with VBA

I need some VBA code to calculate the subtotals for each group in Column A that has a $$balance in column F  but with the subtotals 3 rows below  the colum total. There is a variable number of groups in Column A- can be up to twenty different group

Total  $204,347.21

Group 1 total:  $103854.21
Group 2 total:    $92358.52
Group 3 total:      $5409.29
Book1.xls
Microsoft DevelopmentMicrosoft Excel

Avatar of undefined
Last Comment
irudyk
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image


Enclosed is your workbook and code where before shows you your data before macro is run and after is once the macro is run..
Saurabh..
Sub subtotal()
Application.ScreenUpdating = False
 
Dim x As String
x = "Totals"
Cells.Find(What:=x, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Delete
Dim rng As Range
Set rng = Range("A1:H" & Cells(65536, "a").End(xlUp).Row)
rng.subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(7), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
MsgBox "Done"
Range("A1").Select
 
Application.ScreenUpdating = True
 
End Sub

Open in new window

Subtotal.xls
Avatar of llawrenceg
llawrenceg
Flag of United States of America image

ASKER

Unfortunately not quite what I needed . The table can not be broken up into subtotals and the results of subtotaling each group can only show up below the totals
Avatar of irudyk
irudyk
Flag of Canada image

I think you could use the SUMIF formula to do what your are looiking for. See the revised attached file for an example:
Book1.xls
Avatar of llawrenceg
llawrenceg
Flag of United States of America image

ASKER

Since the column of data items will be of variable length i need some vab that will adjust to various lengths
Avatar of irudyk
irudyk
Flag of Canada image

Not clear what you mean by "lengths".  If you mean that the number of data rows may increase or decrease, then you could modify the SUMIF formula to start at the heading row and end at the Total row.  If that is done, then the formulas for the group totals will expand and contract as rows are added/deleted.  See revised example file.
Book1.xls
Avatar of llawrenceg
llawrenceg
Flag of United States of America image

ASKER

Great work but I need it in VBA.
Avatar of llawrenceg
llawrenceg
Flag of United States of America image

ASKER

Also  he label or title for each group would need to be added
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo