Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

summing arrays to each other 3

Status
Not open for further replies.

jluft

Technical User
May 4, 2004
74
US
is there a way to take arrays of equal size and add them to one another (without looping)?

ie.
array1(1,1,1,1)
array2(2,2,2,2)

goal would be to set
array3 to equal (3,3,3,3)

thanks
 
Hi jluft,

You seem to have asked several questions on similar lines and the answer is, in general, no.

In some (limited) circumstances it is possible to use Excel Worksheet Functions on arrays and/or to copy them to cell ranges where you may be able to do what you want before copying them back but if you don't understand what you're doing, you'll likely get it wrong.

In the general case you must use a loop of some sort. If you're doing a lot of this kind of thing, consider writing your own function.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 

j,

What's with the crusade to bypass loops?

Loops are your friend.

Programmers use loops almost without thinking, but if yoju want to make a generic SumArrays function then...
Code:
Function SumArrays(Ary1, Ary2)
    Dim Ary3(), i, j
    i = LBound(Ary1) + UBound(Ary1)
    j = LBound(Ary2) - LBound(Ary1)
    If i = LBound(Ary2) + UBound(Ary2) Then
        ReDim Ary3(LBound(Ary1) To UBound(Ary1))
        For i = LBound(Ary1) To UBound(Ary1)
            Ary3(i) = Ary1(i) + Ary2(i + j)
        Next
        SumArrays = Ary3
    Else
        SumArrays = Array(0)
    End If
End Function

Skip,

[glasses] [red]Be advised:[/red]To be safe on the FOURTH, don't take a FIFTH on the THIRD, or...
You might not come FORTH on the FIFTH! [bomb][tongue]
 
the reason i am trying to circumvent the use of loops is that i have millions of pieces of data that need to be aggregated and it takes a long time to do this within the confines of excel (ie without the aid of database)
would writing a function here work any faster? im guessing that the answer is no
 
If you really do have "millions of pieces of data", you are far better off using a database to manipulate them - it will have far more efficient matching and sorting routines etc

Rgds, Geoff

Three things are certain. Death, taxes and lost data. DPlank is to blame

Please read FAQ222-2244 before you ask a question
 
What is the source of these millions of numbers? They don't start out in arrays. Perhaps if you tell us what you're trying to do someone will help you find the best way to do it.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
i work at a securities firm. we have a vendor suppling us various attributes of securities that occur monthly over 30 years on thousands of securities across multiple scenarios
approx 500 x 360 x 10 x 5

i am placing the data which is given to us in spreadsheet form into an array. this is very fast
i am then creating subsets of this data that are derived from the parsed components
as i will almost always want to take all-or-none of the data depending on certain criteria, it would be far more efficient if i were able to take an entire portion of an arraty and aggregate it rather than having to take each component individually

that is why i have been asking all of these questions pertaining to arrays

on a "larger" portfolio this process takes approx 25-30 mins as is. if i could manipulate these arrays as i am hoping to i feel this could be cut down to 1-2 mins

thanks for any feedback in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top