View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Darren Hill Darren Hill is offline
external usenet poster
 
Posts: 47
Default Function To Combine two 3-D arrays

Alan Beban wrote:
Darren Hill wrote:

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


The above doesn't seem to have anything to do with 3-d arrays, only 2-D;
no reference to an array element includes 3 index numbers. Why is the
variable k declared? It is not used. Why is the variable ArrNew
declared? It is not used.

What's up???

Alan Beban


Oops, you're right: it's a 2D array with three elements in the first
array. That'll teach me to post when I'm suffering from
looking-at-code-too-long fatigue :)

The k variable and arrnew are there because I originally planned to move
the two arrays into a third array and thought I might need them. Then I
realised I could stick with the original arr1. I should have removed them.

Darren