I am searching through a two-dimensional Variant array to find values that satisfy a condition (in this case if the product is in the Inventory for example), and then if the condition is true, I want to assign the rest of the "row" to a new array.
The array I have as input to the function is a Variant/Variant(1 to 100, 1 to 4) and doesn't have headers *note: the length is variable, so for instance 100 can then become 10 or 1000, but the width 4 remains a constant
My code so far:
Function sortingArray( arr As Variant )
Dim counter As Integer
Dim i As Long
Dim j As Variant
Dim z As Variant
Dim inventory As Variant
'if I declare it as: Dim inventory(0,1) As Variant it does not compile
Dim soldItems As Variant
cntr = 0
For i = LBound(arr) To UBound(arr)
If InStr(arr(i,2), "Inventory") Then
j = arr(i,3)
z = arr(i,4)
If counter = 0 Then
inventory(0,0) = j
inventory(0,1) = z
Else
ReDim Preserve inventory(counter + 1,1)
inventory(counter + 1,0) = j
inventory(counter + 1,1) = z
End If
counter = counter + 1
End If
Next i
sortingArray = inventory
End Function
Example Array:
| result | Inventory_ProdA | A1 | 5.468 |
|---|---|---|---|
| result | Inventory_ProdA | 4/z | 10.6704 |
| result | Inventory_ProdA | b24-0 | 0.567 |
| result | Inventory_ProdA | V3 | 1.2 |
| result | Sold_ProdA | L2 | 8.32 |
| result | Sold_ProdA | A1 | 13.450 |
| result | Sold_ProdA | KP/09 | 8.32 |
| result | Sold_ProdA | V3 | 13.450 |
| result | Sold_ProdA | b24-0 | 46.08 |
| result | Sold_ProdA | 4/z | 2.8370 |
| result | Sold_ProdA | 78-ir-8 | 0.0672 |
| result | Inventory_ProdB | A1 | 0.3 |
| result | Inventory_ProdB | 4/z | 0.05801 |
| result | Inventory_ProdB | b24-0 | 1.0129 |
| result | Inventory_ProdB | V3 | 5.779 |
| result | Inventory_ProdB | KP/09 | 18.99 |
| result | Sold_ProdB | L2 | 2.355 |
| result | Sold_ProdB | A1 | 0.62 |
| result | Sold_ProdB | 4/z | 32.011 |
| result | Sold_ProdB | KP/09 | 15.66 |
Essentially my issue is i cannot resize the new array properly to accommodate the values i assign to it.
As the final goal it is to get a resulting table that as a first column it has non duplicate values of the third column of the initial array arr(i,3) and as a second column it has the sums of arr(i,4) for each of the arr(i,3) if arr(i,2) = inventory, and as a third column it has same but for arr(i,2) = sold
So it would look like this:
| group code | Inventory | Sold |
|---|---|---|
| A1 | Sum of arr(i,4) if Inventory and A1 | Sum of arr(i,4) if Sold and A1 |
| 4/z | Sum of arr(i,4) if Inventory and 4/z | Sum of arr(i,4) if Sold and 4/z |
| b24-0 | Sum of arr(i,4) if Inventory and b24-0 | Sum of arr(i,4) if Sold and b24-0 |
| V3 | Sum of arr(i,4) if Inventory and V3 | Sum of arr(i,4) if Sold and V3 |
| L2 | Sum of arr(i,4) if Inventory and L2 | Sum of arr(i,4) if Sold and L2 |
| KP/09 | Sum of arr(i,4) if Inventory and KP/09 | Sum of arr(i,4) if Sold and KP/09 |
| 78-ir-8 | Sum of arr(i,4) if Inventory and 78-ir-8 | Sum of arr(i,4) if Sold and 78-ir-8 |




