Try this one. The macro below put the sum of each code from column T.
Sub Total_by_code()
Const CodeCol = "J" 'Column where your Code reside
Const ValueCol = "R" 'Column where your Value reside
Const StotalCol = "T" 'Column where sum of each code are put in from
Dim CodeRng As Range, ValueRng As Range, StotalRng As Range
Dim StotalCell As Range, NextCell As Range, rng As Range
Dim startrow As Long, lastrow As Long
Dim Col As Variant, ncol As Long
startrow = 1 'first row's number where data reside
lastrow = Cells(Rows.Count, CodeCol).End(xlUp).Row
Set CodeRng = Range(Cells(startrow, CodeCol), Cells(lastrow, CodeCol))
Set ValueRng = Range(Cells(startrow, ValueCol), _
Cells(lastrow, ValueCol))
Set StotalRng = Range(Cells(startrow, StotalCol), _
Cells(startrow, Columns.Count))
Set StotalCell = Cells(startrow, StotalCol)
StotalRng.Resize(2).ClearContents
For Each rng In CodeRng
If StotalCell = "" Then
StotalCell = rng.Value
StotalCell.Offset(1, 0).Formula = "=sumif(" & _
CodeRng.Address(False, False) & ",""" & rng.Value & _
"""," & ValueRng.Address(False, False) & ")"
Set NextCell = StotalCell.Offset(0, 1)
Else
Col = Application.Match(rng.Value, StotalRng, 0)
If IsError(Col) Then Col = 0
If Col > 0 Then
StotalCell.Offset(0, Col - 1) = rng.Value
StotalCell.Offset(1, Col - 1).Formula = "=sumif(" & _
CodeRng.Address(False, False) & ",""" & rng.Value & _
"""," & ValueRng.Address(False, False) & ")"
Else
NextCell = rng.Value
NextCell.Offset(1, 0).Formula = "=sumif(" & _
CodeRng.Address(False, False) & ",""" & rng.Value & _
"""," & ValueRng.Address(False, False) & ")"
Set NextCell = NextCell.Offset(0, 1)
End If
End If
Next
End Sub
Keiji