Consulting

Results 1 to 5 of 5

Thread: Filling an Array works only with certain ranges...why?

  1. #1
    VBAX Contributor compariniaa's Avatar
    Joined
    Jun 2006
    Location
    Santa Clarita, CA
    Posts
    117
    Location

    Filling an Array works only with certain ranges...why?

    I'm trying to populate an array with 119 values from a range. The range is in O62:O181. When I do a

    for r = 62 to 181
    loop, I get a "Subscript out of Range" error
    however, when I change it to

    For r = 1 to 119

    there's no problem. Furthermore, neither of these work if the proper sheet is not selected.

    I've included a workbook with dummy data and my affected procedure. Any help is much appreciated

  2. #2
    VBAX Contributor compariniaa's Avatar
    Joined
    Jun 2006
    Location
    Santa Clarita, CA
    Posts
    117
    Location
    thanks to anyone who took a look. turns out i did the math wrong and should have made my array 120 items instead of 119

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Thanks for posting your solution.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    VBAX Contributor Aaron Blood's Avatar
    Joined
    Sep 2004
    Location
    Palm Beach, Florida, USA
    Posts
    130
    Location
    OK... I'll ask the question.

    Why are you stepping thru the cells of a range to assign the values to an array? A range IS an array.

    If you dim your array as a variant you don't even have to specify upper and lower bounds. As a bonus, I'll throw in the fact that you can dump arrays straight to a cell and automatically size the output range to fit the array structure.

     
    Dim MyArray As Variant
     
    Sub LoadArray()
        MyArray = Range("O62:O181").Value
    End Sub
     
    Sub DumpArray()
        Range("A1").Resize(UBound(MyArray, 1), UBound(MyArray, 2)).Value = MyArray
    End Sub

  5. #5
    VBAX Contributor compariniaa's Avatar
    Joined
    Jun 2006
    Location
    Santa Clarita, CA
    Posts
    117
    Location
    I had no idea....thanks Aaron!

Posting Permissions

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