Ok, first up, I wouldn't usually do this with code, I'd either use Excels built in subtotals or a pivot table - you might want to consider that..
Anyway, that aside, here is some code that will do what you need :
Sub Collate_Data(sSheet As String, i1 As Integer, i2 As Integer)
' Check how many different elements there are
Dim dNum() As Double
ReDim dNum(i1 To i2)
dNum(i1) = Sheets(sSheet).Cells(i1, 2).Value
iInc = i1
For iLoop = i1 To i2
bMatch = False
For iLoop2 = i1 To i2
If dNum(iLoop2) = Sheets(sSheet).Cells(iLoop, 2).Value Then bMatch = True
Next
If bMatch = False Then
iInc = iInc + 1
dNum(iInc) = Sheets(sSheet).Cells(iLoop, 2).Value
End If
Next
ReDim Preserve dNum(i1 To iInc)
' Create New table entries
For iLoop = LBound(dNum) To UBound(dNum)
Sheets(sSheet).Cells(iLoop, 8).Value = dNum(iLoop)
For iLoop1 = i1 To i2
If Sheets(sSheet).Cells(iLoop1, 2).Value = dNum(iLoop) Then
Sheets(sSheet).Cells(iLoop, 9).Value = Sheets(sSheet).Cells(iLoop, 9).Value + Sheets(sSheet).Cells(iLoop1, 3).Value
Sheets(sSheet).Cells(iLoop, 10).Value = Sheets(sSheet).Cells(iLoop, 10).Value + Sheets(sSheet).Cells(iLoop1, 4).Value
End If
Next iLoop1
Next
End Sub
This code assumes that you are passing it the sheet name, first row that the data is in and the last row that the data is in
The column number has been assumed as 2 for the Ord No.
The result data is written to the right in columns 8 9 and 10
Additional parameters can easily be added if you want this to change often..
|