D
Darren Hill
I have a function to combine two 3d arrays. Is there a more efficient
way to do this than the code below?
Note: there's a complication:
The 3rd dimension includes a price. When combining the arrays, if the
1st and 2nd dimension in both arrays match, and only then, the 3rd
dimensions are added together.
Thanks,
Darren
Function Combine3DArrays(arr1 As Variant, arr2 As Variant) As Variant
Dim i As Integer, j As Integer, k As Integer
Dim arrNew As Variant
For i = LBound(arr1, 2) To UBound(arr1, 2)
For j = LBound(arr2, 2) To UBound(arr2, 2)
If arr1(1, i) = arr2(1, j) And arr1(2, i) = arr2(2, j) Then
arr1(3, i) = arr1(3, i) + arr2(3, j)
arr2(1, j) = vbNullString
arr2(2, j) = vbNullString
arr2(3, j) = 0
End If
Next j
Next i
For j = LBound(arr2, 2) To UBound(arr2, 2)
If arr2(1, j) <> vbNullString Then
ReDim Preserve arr1(LBound(arr1, 2) To UBound(arr1, 2) + 1)
arr1(1, UBound(arr1, 2)) = arr2(1, j)
arr1(2, UBound(arr1, 2)) = arr2(2, j)
arr1(3, UBound(arr1, 2)) = arr2(3, j)
End If
Next j
Combine3DArrays = arr1
End Function
way to do this than the code below?
Note: there's a complication:
The 3rd dimension includes a price. When combining the arrays, if the
1st and 2nd dimension in both arrays match, and only then, the 3rd
dimensions are added together.
Thanks,
Darren
Function Combine3DArrays(arr1 As Variant, arr2 As Variant) As Variant
Dim i As Integer, j As Integer, k As Integer
Dim arrNew As Variant
For i = LBound(arr1, 2) To UBound(arr1, 2)
For j = LBound(arr2, 2) To UBound(arr2, 2)
If arr1(1, i) = arr2(1, j) And arr1(2, i) = arr2(2, j) Then
arr1(3, i) = arr1(3, i) + arr2(3, j)
arr2(1, j) = vbNullString
arr2(2, j) = vbNullString
arr2(3, j) = 0
End If
Next j
Next i
For j = LBound(arr2, 2) To UBound(arr2, 2)
If arr2(1, j) <> vbNullString Then
ReDim Preserve arr1(LBound(arr1, 2) To UBound(arr1, 2) + 1)
arr1(1, UBound(arr1, 2)) = arr2(1, j)
arr1(2, UBound(arr1, 2)) = arr2(2, j)
arr1(3, UBound(arr1, 2)) = arr2(3, j)
End If
Next j
Combine3DArrays = arr1
End Function