Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default Arrays and Strings

I'm trying to create a function that can compare all the values in a
2-D array along the first parameter of the second dimention and then,
if there is a match, add together the elements in both arrays into a
third - except for the two elements in commen, with a complication that
the last element goes into the a different element in the merged array
--- see below

Function BuildArray(a1,a2)
Dim a3(1,1)
count = 0
For i = 1 to UBound(a1,1)
For j = 1 to UBound(a2,1)
If a2(j,1) = a1(i,1) Then
count = count+1
ResizeArray a3,vcount,2
a3(vcount,1) = a1(i,1) & a1(i,2) & a2(i,2) & a2(i,3)
a3(vcount,2) = a2(i,4)
End If
Next j
Next i
End Function

The complication is that I know that the first array passed (a1) will
always have a dimension of (n,2) but the second can vary (but will
always be at least 2)...

If a2 only has 2 dimensions then I'd want
a3(vcount,1) = a1(i,1) & a1(i,2)
a3(vcount,2) = a2(i,2)

If a2 had more than 2 dimensions then I'd want
a3(vcount,1) = a1(i,1) & a1(i,2) & a2(i,2) & a2(i,n-1)
a3(vcount,2) = a2(i,n)

I'm calling this quite often and right now I have it listed out in
specific detail over and over again (roughly 25 times)
and that tells me a function might be more useful and clear.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Arrays and Strings

wrote:
I'm trying to create a function that can compare all the values in a
2-D array along the first parameter of the second dimention and then,
if there is a match, add together the elements in both arrays into a
third - except for the two elements in commen, with a complication that
the last element goes into the a different element in the merged array
--- see below

Function BuildArray(a1,a2)
Dim a3(1,1)
count = 0
For i = 1 to UBound(a1,1)
For j = 1 to UBound(a2,1)
If a2(j,1) = a1(i,1) Then
count = count+1
ResizeArray a3,vcount,2
a3(vcount,1) = a1(i,1) & a1(i,2) & a2(i,2) & a2(i,3)
a3(vcount,2) = a2(i,4)
End If
Next j
Next i
End Function

The complication is that I know that the first array passed (a1) will
always have a dimension of (n,2) but the second can vary (but will
always be at least 2)...

If a2 only has 2 dimensions then I'd want
a3(vcount,1) = a1(i,1) & a1(i,2)
a3(vcount,2) = a2(i,2)

If a2 had more than 2 dimensions then I'd want
a3(vcount,1) = a1(i,1) & a1(i,2) & a2(i,2) & a2(i,n-1)
a3(vcount,2) = a2(i,n)

I'm calling this quite often and right now I have it listed out in
specific detail over and over again (roughly 25 times)
and that tells me a function might be more useful and clear.


I have no idea what vcount is, but you might consider something like the
following:
On Error Resume Next

'Loop until an error occurs
i = 1
Do
z = UBound(a2, i)
i = i + 1
Loop While Err = 0

'Reset the error value for use with other procedures
Err = 0

'Return the number of dimensions
k = i - 2

If k=2 then
a3(vcount,1) = a1(i,1) & a1(i,2)
a3(vcount,2) = a2(i,2)
Else
a3(vcount,1) = a1(i,1) & a1(i,2) & a2(i,2) & a2(i,k-1)
a3(vcount,2) = a2(i,k)
EndIf

Or, if you regularly have the array functions from my web site available
to your workbook, you could use k = ArrayDimensions(a2) instead of the loop.

Alan Beban
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Arrays and Strings

I don't follow you he

If a2 had more than 2 dimensions then I'd want
a3(vcount,1) = a1(i,1) & a1(i,2) & a2(i,2) & a2(i,n-1)
a3(vcount,2) = a2(i,n)


The number of dimensions refers to the number of subscripts inside the
parentheses, not to the upper or lower bounds of those subscripts. In your
example, a2 is a two-dim array.
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
find and replace numeric strings in larger text strings Mr Molio Excel Worksheet Functions 8 November 9th 11 05:17 PM
How to find number of pairs of strings from list of strings? greg_overholt Excel Worksheet Functions 5 January 27th 06 10:42 PM
How can I count strings within strings Paul W Excel Worksheet Functions 4 June 14th 05 12:39 PM
Finding strings within strings Rod[_6_] Excel Programming 1 December 2nd 03 05:34 PM
Finding strings within strings Rod[_6_] Excel Programming 1 December 2nd 03 05:19 PM


All times are GMT +1. The time now is 02:23 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"