sub-total code.


Posted by Colin on October 12, 2001 8:07 AM

I am trying to find a macro to calculate the subtotal since the last instance of sub-total. This is a code I got from an earlier posting but it doesn't seem to work. I changed the variables to suit my spreadsheet. But it didn't do anything in the spreadsheet.
I need the code to print the formulas in the sheet so that any changes that are made will be reflected in the totals. The total should only take into account the records since the last sub-total. I don't even know if this code is right.

Any help would be greatly appreciated.

Thanks.

Sub SubTotal()
'this should do it but you will have to change three things
'to suit your needs
'run this code on a copy of your worksheet

Dim MyCol
'change 6 to column you want subtotals in
'where A = 1, B = 2, C = 3. . .
MyCol = 7

Dim SubCol
'change 1 to the column number that contains "Sub-Total"
'where A = 1, B = 2, C = 3. . .
SubCol = 1

Dim ValCol
'change 3 to the column number that you want to use for subtotals
'where A = 1, B = 2, C = 3. . .
ValCol = 3


'****************************************
'Don't change anything below this point
'****************************************

Dim MySub
MySub = 0
Cells(1, MyCol).Select
Do While Selection.Row <> ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row + 1
If Cells(Selection.Row, SubCol) = "Sub-Total" Then
MySub = MySub + Cells(Selection.Row, ValCol)
Selection = MySub
Selection.Offset(1, 0).Select
Else
Selection.Offset(1, 0).Select
End If
Loop
End Sub



Posted by Barrie Davidson on October 12, 2001 10:17 AM

Colin, what was your earlier posting?
Barrie Davidson