PDA

View Full Version : [SOLVED:] Subtotals



blackie42
04-12-2013, 07:54 AM
Hi,

In column A is the product , column B the cost.

I have sorted the sheet so it has the products in order - there are a number of products and the number of instances varies. So product A could have 3 entries, product B 10, Product C 6 etc.

I have managed to insert 3 lines in between each product group and would like to subtotal the cost in coulmn B using a macro. (simply beacause there are around 2500 entries in total)

Any ideas on how to do this please. Tried using autosum but can't copy this in code cos it uses cell references.

thanks
Jon

GarysStudent
04-12-2013, 05:29 PM
How about:


Sub SubTotalGenerator()
Dim I As Long, N As Long, ST As Long
N = Cells(Rows.Count, "A").End(xlUp).Row
ST = 0
For I = 2 To N
If Cells(I, 1).Value <> "Subtotal" Then
ST = ST + Cells(I, 2).Value
Else
Cells(I, 2) = ST
ST = 0
End If
Next I
End Sub

blackie42
04-13-2013, 01:16 AM
Good stuff mate -works fine on test sheet.

Will try on proper worksheet monday.

thanks very much for your help.

regards
Jon

GarysStudent
04-13-2013, 03:05 AM
Thanks for the feedback!