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:
Then to test that the array had the data in it; I randomly selected an element, like so:Dim List As Variant List = Sheets("People").Range("A3").End(xlDown)
Can anyone see where I went wrong, or what element I am missing?Dim RandomNumber As Integer For count = 1 To 3 RandomNumber = Int((6 - 1 + 1) * Rnd + 1) MsgBox "There are " & Application.WorksheetFunction.count(List) & " elements in this array" MsgBox "The number " & RandomNumber & " person in the List Array is: " & List(RandomNumber) Next count
Last edited by AErmie; 05-04-2009 at 05:13 PM. Reason: Issue Solved
You're referencing a single cell.
Dim vList As Variant With Sheets("People") vList = .Range("A3", .Range("A3").End(xlDown)).Value End With
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
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?
Pretty hard to address in the abstract. How about a concrete example?now when I try to reference a random item within the array nothing is returned
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Like I placed in my first post, I was testing the contents of the array by randomly checking an elelment. Like so:
But the issue is that the array is cleared after another function is called. Here is the full layout of the code:Dim RandomNumber As Integer For count = 1 To 3 RandomNumber = Int((6 - 1 + 1) * Rnd + 1) MsgBox "There are " & Application.WorksheetFunction.count(List) & " elements in this array" MsgBox "The number " & RandomNumber & " person in the List Array is: " & List(RandomNumber) Next count
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.Dim List As Variant public function StartCode call FillArray call TestArray end function public function FillArray With Sheets("People") vList = .Range("A3", .Range("A3").End(xlDown)).Value End With end function public function TestArray Dim RandomNumber As Integer For count = 1 To 3 RandomNumber = Int((6 - 1 + 1) * Rnd + 1) MsgBox "There are " & Application.WorksheetFunction.count(List) & " elements in this array" MsgBox "The number " & RandomNumber & " person in the List Array is: " & List(RandomNumber) Next count end function
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?
Put Option Explicit at the top of your module and try again.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
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:
However, this code generates an error.public function TestArray Dim RandomNumber As Integer For count = 1 To 3 RandomNumber = Int((6 - 1 + 1) * Rnd + 1) MsgBox "The number " & RandomNumber & " person in the List Array is: " & List(RandomNumber) Next count end function
- Error: "Run-time Error '9': Subscript out of range"
Any thoughts about the way I am trying to test the contents of the 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
MsgBox "The number " & RandomNumber & " person in the List Array is: " & List(RandomNumber, 1)
Thanks, that was the issue. Not sure why I didn't catch that, but thanks again!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks