PDA

View Full Version : Solved: Store a sequence of numbers in an array and recall them as required.



cchris_uk
02-11-2008, 10:52 AM
Is it possible to setup a string, or dim an array with for example 7 values, and then using a loop, extract each value one by one from that string/array?

so in crude terms:

Dim list as array
Dim i as string
Dim value as string
list = (2, 4, 8, 16, 4, 50, 1)
for i = 1 to 7
let value = array(i)
msgbox value
next i

This would display 7 msgboxes, each with the numbers defined in 'list'.

Chris

Bob Phillips
02-11-2008, 11:02 AM
Dim list As Variant
Dim i As Long

list = Array(2, 4, 8, 16, 4, 50, 1)
For i = LBound(list) To UBound(list)

MsgBox list(i)
Next i

cchris_uk
02-11-2008, 12:02 PM
Oh blimey, that was embarassingly easy :eek:.

Thanks again xld!

Chris

xluser2007
02-12-2008, 03:40 AM
xld, just a side question:

If we wanted to loop through the array in the order of elements listed i.e. (2, 4, 8, 16, 4, 50, 1), as opposed to [1,2,4,4,8,16,50] which is what the code you posted is doing, how do we do that?

cchris_uk
02-12-2008, 04:13 AM
The code appears to work fine for me.

I get 7 msgboxes with the numbers 2, 4, 8, 16, 4, 50 and 1 in that order.

Chris

Bob Phillips
02-12-2008, 11:38 AM
xluser2007,

As Chris has found, my code iterates through the array in the same order that it is loaded. When an array is loaded, each value just takes the next slot, no automatic sorting, and when you retrieve them LBound to UBound, it goes back through in the same order.