Array Help Requested (1 Viewer)

XLEAccessGuru

XLEGuru
Local time
Today, 02:23
Joined
Nov 17, 2006
Messages
65
I've finally begun to understand arrays but am still only 1 day old with them. I got one to work in my code, but have a couple questions I hope you VBA buffs can help me with.

1. The MSDN website has a help page discussing a Binary Search Function for arrays that is similar to the array text search here:

----> http://http://msdn2.microsoft.com/en-us/library/aa164795(office.10).aspx

However, they offer no example. Does anyone have an example of the binary search or a similar function that searches numeric arrays for a specific number (or numbers)?


2. MSDN does have an example of the string array search function (http://msdn2.microsoft.com/en-us/library/aa164799(office.10).aspx) but silly me can't seem to get it to work right. Can someone offer an example of how this works with an array?

3. How can I see all of the values in my array after I create it either in a text box or in the immediate window (I want to check that it is working right)? I tried this:

Code:
Debug.Print MyArray(i)

but it doesn't work . I get a "Subscript out of Range" error.

Any help is much appreciated! :)
 

llkhoutx

Registered User.
Local time
Today, 01:23
Joined
Feb 26, 2001
Messages
4,018
I've always thought of arrays of being similar to a mail pigeon hole sorter.

To debug.print out all entries in an array, loop through the array; it may be multi-dimensional which is no big deal.
 

Moniker

VBA Pro
Local time
Today, 01:23
Joined
Dec 21, 2006
Messages
1,567
In Debug.Print MyArray(i), the variable "i" isn't set to anything, meaning it's NULL, meaning the subscript is out of range.

To see what's in your array and see it in the immediate window, you need to know the upper bound of your array, and then, as suggested, loop through it. It'll look something like this:

Code:
Sub cmdListArray_Click()

Dim i as Integer

For i = 0 to <your_upper_bound_number here>
    Debug.Print MyArray(i)
Next

End Sub

Attach the above code to the click event of a command button named cmdListArray.

Your upper bound number is the total number of entries in your array, minus one (because arrays start at zero). If you want to just see what's in a specific entry, you can use the immediate window. For example, if you want to see the third entry in your array, you'd type this in the immediate window:

Debug.Print MyArray(2)

You use 2 because array indexes are zero-bound, meaning 0 = first array entry, 1 = second array entry, and so on.
 

Users who are viewing this thread

Top Bottom