paul.robinson@it-tallaght.ie
Guest
Re: Collect data and sum, w/ connection to another sheet
Hi
You could maintain collections.
You have 17 columns of data per row. I'll assume the invoice number is
in the first column. I'll assume your data starts in row 2 (row1 is a
header row?) and has a range name called "InvoiceData" which includes
the header row.
The function below outputs a collection of "rows" with the sums in.
Function MakeSums() As Collection
Dim DataVariant As Variant
Dim Datarows As Long
Dim TempSums(1 To 1, 1 To 17) As Variant
Dim TempVariant As Variant
Dim i As Long, j As Long
Dim TestCollection As New Collection, SumCollection As New Collection
DataVariant = ActiveSheet.Range("InvoiceData").Value
Datarows = UBound(DataVariant, 1)
On Error Resume Next
For i = 2 To Datarows
Err.Clear
TestCollection.Add DataVariant(i, 1), Trim(CStr(DataVariant(i,
1)))
If Err.Number = 0 Then
For j = 1 To 17
TempSums(1, j) = DataVariant(i, j)
Next j
TempVariant = TempSums
Else
TempVariant = SumCollection(Trim(CStr(DataVariant(i, 1))))
SumCollection.Remove Trim(CStr(DataVariant(i, 1)))
For j = 2 To 17
TempVariant(1, j) = TempVariant(1, j) + DataVariant(i,
j)
Next j
End If
SumCollection.Add TempVariant, Trim(CStr(DataVariant(i, 1)))
Next i
Set MakeSums = SumCollection
End Function
The sub below writes these rows to the "Summary Sheet", again starting
at row 2.
Sub OutputSums()
Dim OutputCollection As Collection
Dim Item As Variant
Set OutputCollection = MakeSums
With Worksheets("Summary Sheet")
i = 2
For Each Item In OutputCollection
.Cells(i, 1).Resize(1, 17).Value = Item
i = i + 1
Next Item
End With
End Sub
regardsPaul
Bookmarks