+ Reply to Thread
Results 1 to 4 of 4

Combine - by summing - two arrays into a third

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-28-2008
    Location
    Reykjavík, Iceland
    MS-Off Ver
    Any of: 2003 & 2007
    Posts
    412

    Combine - by summing - two arrays into a third

    Hi,

    I know a few ways to accomplish the task but I'm curious to know if there is a syntax which will "Do it My Way".
    The following code will fill Arr1 cells with 10 values from range A1:A10 and then fill F1:F10 with those values.

    Sub FODA()                
      Dim Rng As Range
      Arr1 = Range("A1:A10")
      Set Rng = Range("F1:F10")
      Rng = Arr1
    End Sub

    Now, assume 2 ranges (A1:A10 and B1:B10)
    Putting each range into an Array is easy - as shown in the 2 first commands of the following code.

    I want to add cell 1 of Arr1 with cell 1 of Arr2 and put the result in cell 1 of Arr3
    I have tried:

    Sub FODA2()
    Dim Rng As Range
      Arr1 = Range("A1:A10")
      Arr2 = Range("B1:B10")
      Arr3 = Arr1 + Arr2    ' Returns "Type Mismatch" error
      Set Rng = Range("F1:F10")
      Rng = Arr3
    End Sub

    Is there a way to fill Arr3 with Arr1 & Arr2 cells sum with a single command (WITHOUT LOOPING) ?.

    'Join' and 'Split' don't seem to do any good.

    Thanks, Elm
    Last edited by ElmerS; 03-25-2010 at 03:34 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Combine - by summing - two arrays into a third

    Quote Originally Posted by ElmerS
    Is there a way to fill Arr3 with Arr1 & Arr2 cells sum with a single command (WITHOUT LOOPING) ?.
    Not that I'm aware of.

    Though this will invariably fall foul of your specific requirements I would (for the benefit of others) suggest using Evaluate...

    Range("F1:F10") = Evaluate("IF(ROW(A1:A10),A1:A10+B1:B10)")
    or if you want to store as an Array

    Arr3 = Application.Transpose(Evaluate("IF(ROW(A1:A10),A1:A10+B1:B10)"))

  3. #3
    Forum Contributor
    Join Date
    11-28-2008
    Location
    Reykjavík, Iceland
    MS-Off Ver
    Any of: 2003 & 2007
    Posts
    412

    Re: Combine - by summing - two arrays into a third

    Thank you.

  4. #4
    Forum Contributor
    Join Date
    11-28-2008
    Location
    Reykjavík, Iceland
    MS-Off Ver
    Any of: 2003 & 2007
    Posts
    412

    Re: Combine - by summing - two arrays into a third

    It seems as if the following will also do:

    [F1:F10] = [A1:A10+B1:B10]

    Elm

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1