Macro to Sub total varying lists of numbers

shredda23

New Member
Joined
Apr 16, 2014
Messages
44
Hi I am fairly new to excel and I am having trouble trying to get a sub total function to work for me. I need the subtotal to for a list of about 20 columns in one line with a varying amount of rows. So the subtotal must stop when there is a blank at the top. I can get the macro to find the totals A on the spreadsheet and then offset to beneath the columns of numbers but the sub total formulas takes in every number. I'm hoping somone can help. Here is the example:


456.00</SPAN>8,798.00</SPAN>
45,465.00</SPAN>789,789.00</SPAN>
45,465.00</SPAN>789,789.00</SPAN>
45,646.00</SPAN>7,897.00</SPAN>
Totals A</SPAN>
45,646.00</SPAN>
456,465.00</SPAN>45,646.00</SPAN>
456,465.00</SPAN>1,231.00</SPAN>
56,456.00</SPAN>45,646.00</SPAN>
5,465.00</SPAN>56,456.00</SPAN>
4,546.00</SPAN>45,646.00</SPAN>
Totals B</SPAN>
456,465.00</SPAN>1,231.00</SPAN>
56,456.00</SPAN>45,646.00</SPAN>
5,465.00</SPAN>56,456.00</SPAN>
4,546.00</SPAN>45,646.00</SPAN>
48,654.00</SPAN>7,987.00</SPAN>
546.00</SPAN>89.00</SPAN>
456.00</SPAN>899.00</SPAN>
Totals C</SPAN>

<TBODY>
</TBODY><COLGROUP><COL><COL><COL></COLGROUP>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN>
</SPAN>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN></SPAN>
</SPAN></SPAN>

<TBODY>
</TBODY><COLGROUP><COL><COL><COL></COLGROUP>
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
If I understand what you want and your data layout correctly, then I think this macro will do what you are asking for...
Code:
Sub DoTotals()
  Dim C As Long, A As Long, ColumnRange As Range, TotalRows As Range, TotalCells As Range, Ar As Range
  Set ColumnRange = Range("B1", Cells.Find("*", , xlValues, , xlByColumns, xlPrevious)).EntireColumn
  Set TotalRows = Union(Rows(1), Columns("A").SpecialCells(xlConstants).EntireRow)
  Set TotalCells = Intersect(TotalRows, ColumnRange)
  For A = 2 To TotalCells.Areas.Count
    For C = 2 To ColumnRange.Columns.Count - 1
      With TotalCells
        Cells(.Areas(A).Row, C).Value = WorksheetFunction.Sum(Range(Cells(.Areas(A - 1). _
                                        Offset(-(.Areas(A - 1).Row <> 1)).Row, C), _
                                        Cells(.Areas(A).Offset(-1).Row, C)))
      End With
    Next
  Next
End Sub
 
Upvote 0
Wow thanks for the help. I was just wondering what part of the code I need to alter say If my data started in column D or E and also the number of columns I needed subtotals was say 20 to 25. Also is it possible to have the sub total formula to show up in the totals cells. Would just need for audit purposes.

Thanks for the help!!!
 
Upvote 0
Wow thanks for the help. I was just wondering what part of the code I need to alter say If my data started in column D or E and also the number of columns I needed subtotals was say 20 to 25. Also is it possible to have the sub total formula to show up in the totals cells. Would just need for audit purposes.

Thanks for the help!!!
I've assumed ..
- that the final column can be determined from headings (or data) in row 1
- that you can change the code to reflect the first column of data to be summed and the column that the 'Totals' labels are in
- that otherwise the layout is as shown here.

Excel Workbook
BCDEFG
1H1H2H3H4
257.0070.0092.0084.00
33.0055.0092.0044.00
468.0051.0052.0047.00
536.0041.0027.006.00
6Totals A
737.0049.0016.0048.00
826.0063.0055.0016.00
994.0066.0051.0040.00
1011.0079.0046.0076.00
1160.0084.002.0022.00
128.0011.0034.0013.00
13Totals B
1483.009.0020.0068.00
1546.0036.0015.0071.00
1693.0054.009.0076.00
1741.0047.0050.0021.00
1833.0010.0059.0017.00
1993.0010.0045.0028.00
2088.0076.0028.0068.00
2126.009.004.0033.00
22Totals C
23
SubTotals



The code ..
Rich (BB code):
Sub DoSubTotals()
  Dim lLastcol As Long, lFDC As Long, lTLC As Long
  Dim rBlank As Range
  
  Const TotalLabelsCol As String = "B"  '<- Change to suit
  Const FirstDataCol As String = "D"    '<- Change to suit
  Const fBase As String = "=SUM(R#C:R[-1]C)"
  
  lLastcol = Cells(1, Columns.Count).End(xlToLeft).Column
  lTLC = Columns(TotalLabelsCol).Column
  lFDC = Columns(FirstDataCol).Column
  For Each rBlank In Columns(lTLC).SpecialCells(xlBlanks).Areas
    rBlank.Offset(rBlank.Rows.Count, lFDC - lTLC).Resize(1, lLastcol - lFDC + 1).FormulaR1C1 _
      = Replace(fBase, "#", rBlank.Row, 1, 1, 1)
  Next rBlank
End Sub



The result (with just one formula per row shown) ..

Excel Workbook
BCDEFG
1H1H2H3H4
257.0070.0092.0084.00
33.0055.0092.0044.00
468.0051.0052.0047.00
536.0041.0027.006.00
6Totals A164.00217.00263.00181.00
737.0049.0016.0048.00
826.0063.0055.0016.00
994.0066.0051.0040.00
1011.0079.0046.0076.00
1160.0084.002.0022.00
128.0011.0034.0013.00
13Totals B236.00352.00204.00215.00
1483.009.0020.0068.00
1546.0036.0015.0071.00
1693.0054.009.0076.00
1741.0047.0050.0021.00
1833.0010.0059.0017.00
1993.0010.0045.0028.00
2088.0076.0028.0068.00
2126.009.004.0033.00
22Totals C503.00251.00230.00382.00
23
SubTotals
 
Upvote 0
Report:

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
MAPSDATA WITH SUBTOTALS

<COLGROUP><COL style="WIDTH: 48pt" span=2 width=64><TBODY>
</TBODY>
Database:

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
Quarter Ending 3/31/2014
Entity

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
Gross Market</SPAN>

<TBODY>
</TBODY><COLGROUP><COL></COLGROUP>
Entity ID

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
Entity Name

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
Type

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
value</SPAN>

<TBODY>
</TBODY><COLGROUP><COL></COLGROUP>
137702

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
gdf

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
CJV

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
2014

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
1

<COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY>
</TBODY>
4,564.00</SPAN>

<TBODY>
</TBODY><COLGROUP><COL></COLGROUP>

<TBODY>
</TBODY>
Hi I think we are almost there. The formula is not working correctly in my workbook but I'm guessing it is because of the way my data is laid out. My total heading is in column b and the totals should start formulating in col f accross to col y. There are headings and report titles in rows 1-10 of the worksheet. When I run the macro you sent me it creates a formula in cell F9(where value is typed in the table above) totaling from F3 to f 8, then again in F106 doing a sub total of f104 to f105 right at the beginning of where my next group of numbers begins and repeats the same thing. It does not do any subtotals in any other column other than F. Is it possible for me to attach my worksheet as It may be easier for you to see and work with? Ive tried to put a sample of the way the beginning of the report should look like above.
 
Upvote 0
It seems to me like your layout is not nearly as simple as your original sample, and that the assumptions I listed are not correct, so it isn't surprising that it is not working as hoped.

You cannot attach an actual file, nor should we need one with hundreds of rows. You can post small screen shots, as I did, using one of the methods suggested in my signature block below.
Ideally you can make up some dummy data that reflects the gist and any variances in your layout but with just say 3 sections, the data to sum in, say columns F:H only and no more than about 4 rows to actually add in each section and post that as suggested. Advantage is that we can not only see row/column labels and actual layout, but can copy the data to a worksheet to test.

It would also be good to know how to determine the last column to sum. My assumption was that it was the last column with something in row 1 but that apparently wasn't the case. Perhaps for you it is another particular row, or do we need to find the last column that has data anywhere in it?
 
Upvote 0
abcdefg
1Report:Title
2Database:title
3
4
5
6
7
8datadata
9Entity IDEntity nameTypeyearQtrGMVEncumbrance
10546546nameCJV2014145645644654654
11546545namecjv20141478794687987
1275465namecjv2014146556465456
13CJV subtotal??
14
15
16465466NameEJV20141546546465456
17454654nameEjv20141879877897987
18446454nameEJv20141445687897
19EJV subtotal??

<TBODY>
</TBODY>
 
Upvote 0
Above is a basic sample of the data. The main differences being that the subtotals needed would go across 20 columns and the number of CJVs would be approx 200, eJVs approx. 120. There will also be around 10 different categories of entity type. Let me know If this helps.
 
Upvote 0
Let me know If this helps.
Yes that helps, but what about this?
It would also be good to know how to determine the last column to sum. My assumption was that it was the last column with something in row 1 but that apparently wasn't the case. Perhaps for you it is another particular row, or do we need to find the last column that has data anywhere in it?
Can I safely look across row 9 to find what the last column actually is?
Or is the number of columns always exactly 20 (starting from column F)?

Also ..

Is row 10 always the first row of actual data to be included in the calculations?
If not, how would I determine what row contained the first data to be added?
 
Upvote 0
The data will always start in row 10 and the number of columns will always be from col f to clomun Y so 20 columns.
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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