I have an array which stores it's values in a sorted list. I have been using this sorted list to organise data, by date in several other spreadsheets.
My source data is a series of 12 worksheets in one workbook. Each worksheet reflecting a single calendar month. Number of transactions/runs is dynamic--averages 60 or so a month so I set a limit to my loop of 200 as this should be more than enough to cover any growth in business.
My current set of data is such that I have several repeat deliveries (different cargo/weights et al. but the same delivery location). I want to consolidate those "repeat"/similar rows into a single entry in the list, sum the number of pieces delivered, weight and delivery cost, and to increment a counter to show the number of repeated deliveries to that respective site.
Example: January, 2016
Delivered from: Delivered to: No. Pieces: Weight: Cost:
Site A Site B 10 100 $120.00
Site A Site C 5 20 $80.00
Site B Site C 2 30 $45.00
Site A Site C 20 460 $375.00
Summary:
Delivered to: No. of Deliveries: No. Pieces: Weight: Cost:
Site B 1 10 100 $120.00
Site C 3 27 510 $500.00
I can think of ways to do this by dumping data to a "scrap" worksheet, however, I want a VBA solution which is "internal" so that no such "scratch pad" is required.
The number of deliveries, in total, is dynamic. The number of repeat deliveries, for any given location, is also dynamic.
I am finding it very difficult to compose an efficient way to consolidate the information in my list with the above parameters as I am still very new to VBA/Excel.
Any suggestions are appreciated, in particular if you have example code--I know what I want, I'm just not sure how to implement it in VBA.
A sample of my array loading and transfer to the list is shown below (with variable definitions et al. omitted).
Set List = CreateObject("System.Collections.SortedList")
'Grab Monthly Data by Route
For Each ws In Worksheets
If ws.Name <> "Summary" Then
Call DeleteHidden 'Delete Hidden Rows/Columns in the active worksheet if any
With ws
'loop through the sheet to 207 (~3x greatest number of deliveries)
For RowCount = 7 To 207
'Check for dates for each row (Month/Day/Year)
d = DateValue(.Cells(RowCount, 1))
If List.Containskey(d) Then
arTemp = List(d)
Else
ReDim arTemp(12)
End If
'Monthly Totals
arTemp(0) = arTemp(0) + .Cells(RowCount, 1) 'Grab Entry Date/Time
arTemp(1) = arTemp(1) + .Cells(RowCount, 2) 'Grab Delivery Date/Time
arTemp(2) = arTemp(2) + .Cells(RowCount, 3) 'Grab PU Location
arTemp(3) = arTemp(3) + .Cells(RowCount, 4) 'Grab PU Street
arTemp(4) = arTemp(4) + .Cells(RowCount, 5) 'Grab PU City/Province/PC
arTemp(5) = arTemp(5) + .Cells(RowCount, 6) 'Grab Del Location
arTemp(6) = arTemp(6) + .Cells(RowCount, 7) 'Grab Del Street
arTemp(7) = arTemp(7) + .Cells(RowCount, 8) 'Grab Del City/Province/PC
arTemp(8) = arTemp(8) + .Cells(RowCount, 9) 'Grab No. Pieces
arTemp(9) = arTemp(9) + .Cells(RowCount, 10) 'Grab Cargo Weight (LBS)
arTemp(10) = arTemp(10) + .Cells(RowCount, 11) 'Grab Cost
'potential add point of a sort and consolidate function if working with the array prior to data being added to the list (but then such would run for each record of each worksheet---seems too inefficient)
arTemp(12) = arTemp(12) + 1
List(d) = arTemp
Next RowCount
Call QuickSort(arTemp, 0, RowCount - 1) 'Sort the Monthly Array at the end of the Month (can manipulate the array but the list is already loaded..how to manipulate/consolidate the list???)
End With
End If
Next