Link to home
Start Free TrialLog in
Avatar of sheets1
sheets1

asked on

Putting values into an array?

I have a loop statement which generates 5 random numbers and puts them into a variable called "value". Now how would i go about putting each value after generated into an array.
After each value goes into the array i would like to perform a check to see if that number already exists in the array.

Ive never worked with arrays - so i really need some help!

thanks
ASKER CERTIFIED SOLUTION
Avatar of caraf_g
caraf_g

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of caraf_g
caraf_g

PS - other experts: the solution is far from elegant and definitely not efficient but I think sheet1 is a new VB user (I hope I'm not insulting you now sheet1!) and I believe that this way of doing it is easy and a good exercise.
An array is like a set of variables with the same name but referred to by an index.

With your example, to create an array do the following at the top of the sub procedure:

Dim my_var(1 To 5)

There are a few are other variations on declaring an array, but this one is suficient. To input a value to an array do the following:

my_var(1) = CInt(Rnd * 100)

Replace my_var(1) with my_var(counter)           This is where counter is the counting variable in a loop. This would give you the option of not having to display 5 similar lines of code when one will suffice.

To check a value with one in an array, do an If statement to test for your conditions. E.g.

If my_var(counter) = compare_value Then msgbox "compare_value is equal to my_var(counter)"

I post this as a comment rather than an answer because I think there is a better way of looking up a value in an array. I think this can be done using collections.
caraf_g,

In your hasteness to answer the question, you forgot about the definition of an array. See my last paragraph. Do you know a way ?
Hello cartii,

Sorry about the haste but I'm actually teaching at the moment so I have to do this in my breaks. But yes, you're right. It can be done with collections.

Dim objCollection As Collection

Set objCollection = New Collection

Dim Value As Integer

'Let's assume you've calculated a new value with your Random Generator

'We'll try to add the new value to the Collection. But it may already be in there. To allow for this, we define a key for each collection element by using CStr on the Value. Then, if the value is already in the collection, the add will fail so we'll have to monitor for this:
On Error Resume Next
Err.Clear
objCollection.Add Value, CStr(Value)
If Err.Number = 457 Then
    'Value already in the collection
    'Do something to re-calculate Value for example and try again.
End If

'If you want to limit yourself to 5 entries:
If objCollection.Count = 5 Then
    'Hurray - Done!
End If

Oh... PS - After the check If Err.Number = 457 you should really re-set the On Error:
On Error Goto 0
So that any other error will cause a normal "crash". Otherwise, errors might go unnoticed.
Avatar of sheets1

ASKER

caraf_g,

youre array steps seems sensible but i need help (code) on:

-how to add values to an array
-how i actually do loop through and check values in an array.

thanks
Avatar of sheets1

ASKER

caraf_g,

youre array steps seems sensible but i need help (code) on:

-how to add values to an array
-how i actually do loop through and check values in an array.

thanks
Avatar of sheets1

ASKER

caraf_g,

youre array steps seems sensible but i need help (code) on:

-how to add values to an array
-how i actually do loop through and check values in an array.

thanks
Avatar of sheets1

ASKER

caraf_g,

youre array steps seems sensible but i need help (code) on:

-how to add values to an array
-how i actually do loop through and check values in an array.

thanks
To define an Array with 5 elements of Integers

Dim ArrayName(1 to 5) As Integer

To loop through this array
Dim intCounter As Integer

For intCounter = 1 To 5
    'Do something with array element number intCounter
Next

To do something with an individual element of the array
(Examples)
ArrayName(intCounter) = 4
MyVariable = ArrayName(intCounter)
If ArrayName(intCounter) = 3 Then
    'Do something
End If

The Array will be defined with 5 elements from the start, so you don't need to "add" values to it.

If you must, however you can always

Dim ArrayName() As Integer

Then let's say your array has already got 5 elements (indices 1 to 5) and you want to add a sixth one:

ReDim Preserve ArrayName(1 To 6) As Integer

The Preserve keyword makes sure you don't lose the elements 1 to 5 that you have already got.

Now - off you go and work out how to piece all of this together.

Good luck!

PS - I'm teaching people to code as one of the things I do for a living so I hate giving people pre-cooked solutions. You have to have something left to work out for yourself.