How to Sum

  • Thread starter Thread starter hon123456
  • Start date Start date
H

hon123456

Dear all,

I have excel data as follows:

NO Pieces
--------- ----------
A001 10 pc
A001 20 pc
A001 30 pc
A002 15 pc
A002 15 pc

NowI want to write a VBA to sum a subtotal for the column
of A001 and A002, the outcomes
should as follows:


NO Pieces Subtotal
--------- ---------- ------------
A001 10 pc
A001 20 pc
A001 30 pc 60pc
A002 15 pc
A002 15 pc 30pc


How can I do that in Excel with VBA or other method ?

Thanks.
 
Sub ABC()
Dim rng as Range, cell as Range
Dim sum as Long
set rng = Range(cells(2,"A"),Cells(2,"A").End(xldown))
for each cell in rng
sum = val(cell.offset(0,1)) + sum
if cell.value <> cell.Offset(1,0).Value then
cell.offset(0,2).Value = sum & " pc"
sum = 0
end if
Next
Cells(1,3).Value = "Subtotal"
End Sub

right now it looks in A2 to see the first A001. Adjust the code to look at
the correct location.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top