#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Question Adding two arrays


    I'm trying to add two arrays together and getting error code is like this:
    For x = 0 to 180
    For y = 0 to 180
    Total = Array 1(1,x,y) + Array2 (1,x,y)
    Next x
    Next y

    I get a subscript out of range error

    I want to add each element of array1 to its corresponding element
    In array2, then have a variable "total" that is the sum of all the elements that are the sum of those two
    Arrays.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    45
    It should not work at all. For a starter, your for/next loops are nested wrong. It should read :

    For x = 0 to 180
    For y = 0 to 180
    Total = Array1(1, x, y) + Array2(1, x, y)
    Debug.Print Array1(1, x, y), Array2(1, x, y), Total
    Next y
    Next x

    If you want to save the total in Array2, you would have to add a fourth dimension to it.

    J.A. Coutts
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    99
    Rep Power
    157
    Your problem stems from youtr looping as suggested above

    the x is 0 and the y loop is 0 the x moves throu its values and ends 1 larger than the loop value

    this means when you try to look at the nexty value you are looking too far and have an out of bounds error

    simply change the order of the next statments

    and as long as you have dimensioned the arrays correctly you should have no problems

    here to help

IMN logo majestic logo threadwatch logo seochat tools logo