Scenario: I am trying to build a sub that: - Gets data from a worksheet - Sums values of a column if the id is the same - Check the last columns for a value decrease (from previous to current) and retrieve the one that changed the most - Delete duplicate columns
Data:
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Name1 | ID1 | Value | Current Level1 | Previous Level1 | Current Level2 | Previous Level2 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 1 | IDTYPE1 | 100 | 9 | 10 | 9 | 11 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 2 | IDTYPE2 | 500 | 10 | 14 | 8 | 14 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 2 | IDTYPE2 | 244 | 9 | 14 | 9 | 14 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 2a | IDTYPE2 | 378 | | | 9 | 14 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 3a | IDTYPE3 | 343 | 3 | 9 | | |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 3 | IDTYPE3 | 127 | 5 | 4 | 4 | 5 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 4s | IDTYPE4 | 213 | | | 4 | 5 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 4a | IDTYPE4 | 241 | 3 | 5 | 4 | 5 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
Output: In theory the output will have the same columns, but duplicate IDs would be aggregated into a single row. And the data for the "Level" columns, would be, for that single row, the lowest value available in the "Current1" (irrespective of each row), aggregated with the lowest level of "Current2" (each associated with their respective "Previous". The name column is to have the first name for that ID that appeared (for "IDTYPE4" it would be "Issue 4s").
Output example:
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Name1 | ID1 | Value | Current Level1 | Previous Level1 | Current Level2 | Previous Level2 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 1 | IDTYPE1 | 100 | 9 | 10 | 9 | 11 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 2 | IDTYPE2 | 1122 | 9 | 14 | 8 | 14 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 3 | IDTYPE3 | 470 | 3 | 9 | 4 | 5 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
| Issue 4s | IDTYPE4 | 454 | 3 | 5 | 4 | 5 |
+----------+---------+-------+----------------+-----------------+----------------+-----------------+
Question: I am trying to do this in form a loop inside an array, as per the code below. But I cannot think of a way to sum up the values if the IDs are the same and still account for the change in "Level". Is there an easier way to do this?
Obs. I tried the SumIf function, but could not properly place it inside the loop.
Code so far:
Dim procdupArray as Variant
Dim loopvar1 as Integer
Dim w as Workbook
Dim valueSum as Long
Set w = ThisWorkbook
procdupArray = w.Worksheets("Sheet1").UsedRange
For loopvar1 = 2 To UBound(procdupArray, 1) - 1
If procdupArray(loopvar1, 2) <> procdupArray(loopvar1 + 1, 2) Then
w.Worksheets("Sheet2").Cells(loopvar1, 1) = procdupArray(loopvar1, 1)
w.Worksheets("Sheet2").Cells(loopvar1, 2) = procdupArray(loopvar1, 2)
w.Worksheets("Sheet2").Cells(loopvar1, 3) = procdupArray(loopvar1, 3)
w.Worksheets("Sheet2").Cells(loopvar1, 4) = procdupArray(loopvar1, 4)
w.Worksheets("Sheet2").Cells(loopvar1, 5) = procdupArray(loopvar1, 5)
w.Worksheets("Sheet2").Cells(loopvar1, 5) = procdupArray(loopvar1, 5)
w.Worksheets("Sheet2").Cells(loopvar1, 5) = procdupArray(loopvar1, 5)
Else
valueSum = procdupArray(loopvar1, 7) + procdupArray(loopvar1 + 1, 7)
If procdupArray(loopvar1, 4) < procdupArray(loopvar1 + 1, 4) then
w.Worksheets("Sheet2").Cells(loopvar1, 4) = procdupArray(loopvar1, 4)
End if
If procdupArray(loopvar1, 5) < procdupArray(loopvar1, 5) then
w.Worksheets("Sheet2").Cells(loopvar1, 5) = procdupArray(loopvar1, 5)
End if
If procdupArray(loopvar1, 6) < procdupArray(loopvar1, 6) then
w.Worksheets("Sheet2").Cells(loopvar1, 6) = procdupArray(loopvar1, 6)
End if
If procdupArray(loopvar1, 6) < procdupArray(loopvar1, 6) then
w.Worksheets("Sheet2").Cells(loopvar1, 6) = procdupArray(loopvar1, 6)
End if
End if
Next loopvar1
Obs2: Another Problem is that this does not properly take into account if there are more than two rows for the same ID.