Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 47
Default Function To Combine two 3-D arrays


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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Function To Combine two 3-D arrays

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
  #3   Report Post  
Posted to microsoft.public.excel.programming
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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine Two Arrays Into One. Tough. ryguy7272 Excel Worksheet Functions 4 May 8th 23 11:45 AM
How to combine Combo Box function with Vlookup function KH Excel Worksheet Functions 2 April 5th 10 01:24 PM
Combine Two Similar Arrays Rob Excel Worksheet Functions 1 November 17th 09 09:31 PM
how to combine an IF Function with a lookup function to determine [email protected] Excel Worksheet Functions 1 December 5th 06 06:09 AM
Working with Arrays, pasing from function to function mikebres Excel Programming 2 April 27th 06 06:33 PM


All times are GMT +1. The time now is 04:32 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"