+ Reply to Thread
Results 1 to 9 of 9

Add/Retrieve Elements From an Array

  1. #1
    Registered User
    Join Date
    04-17-2009
    Location
    St. Catharines, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    13

    Post Add/Retrieve Elements From an Array

    Hello All,

    I can't seem to get my head around the simple issue of assigning a range of cell data to an array! Here is a snippet of what I have, maybe you can see the flaw:
    Please Login or Register  to view this content.
    Then to test that the array had the data in it; I randomly selected an element, like so:
    Please Login or Register  to view this content.
    Can anyone see where I went wrong, or what element I am missing?
    Last edited by AErmie; 05-04-2009 at 05:13 PM. Reason: Issue Solved

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Add/Retrieve Elements From an Array

    You're referencing a single cell.
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    04-17-2009
    Location
    St. Catharines, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    13

    Post Re: Add/Retrieve Elements From an Array

    Sorry for the no reply for a bit there. Thanks, I tested the code and watched the variables and it does collect the items successfully (thanks). However, now when I try to reference a random item within the array nothing is returned.

    In watching/stepping through the code, I discovered that after the items are placed in the array it is cleared for some reason. One thing that I did not mention in my previous post is that the 2 code segments (the one to fill the array, and the other to randomly select an element from it) are in 2 separate functions. However, I do not see why this would be an issue, as I have declared the array variable as a global variable.

    Any thoughts as to why the content in the array would clear after calling another function?

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Add/Retrieve Elements From an Array

    now when I try to reference a random item within the array nothing is returned
    Pretty hard to address in the abstract. How about a concrete example?

  5. #5
    Registered User
    Join Date
    04-17-2009
    Location
    St. Catharines, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    13

    Post Re: Add/Retrieve Elements From an Array

    Like I placed in my first post, I was testing the contents of the array by randomly checking an elelment. Like so:

    Please Login or Register  to view this content.
    But the issue is that the array is cleared after another function is called. Here is the full layout of the code:

    Please Login or Register  to view this content.
    When the array is filled within the given function, it is fine. All the data is present. But when it leaves that function, and ultimately calls the TestArray function it is blank and returns nothing.

    My question is why does the array clear once leaving the FillArray function since the variable for the array is declared as a global variable?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Add/Retrieve Elements From an Array

    Put Option Explicit at the top of your module and try again.

  7. #7
    Registered User
    Join Date
    04-17-2009
    Location
    St. Catharines, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    13

    Post Re: Add/Retrieve Elements From an Array

    Ok I have added the Option Explicit declaration at the top of the module. However, in order to check the array contents I am trying to select an item from it.

    Here is the code:

    Please Login or Register  to view this content.
    However, this code generates an error.
    - Error: "Run-time Error '9': Subscript out of range"

    Any thoughts about the way I am trying to test the contents of the array?

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Add/Retrieve Elements From an Array

    Assuming you followed shg's advice you would have discovered that whilst you delcared List as public to the module you where actually loading a variable vList in your FillArray routine.

    After correcting that you would then find whilst running the the use of List was incorrect in TestArray routine as the array returned from a range is a 2 dimensional array, rows/columns

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  9. #9
    Registered User
    Join Date
    04-17-2009
    Location
    St. Catharines, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    13

    Post Re: Add/Retrieve Elements From an Array

    Thanks, that was the issue. Not sure why I didn't catch that, but thanks again!

+ 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