x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 192

# 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
0
sheets1
• 5
• 4
• 2
1 Solution

Commented:
1 fill the array with values that you can be sure will not be generated in your random generator. For example, if your random generator generates numbers from 0 to 4, fill the array with -1s.

2 Declare a "flag" field (for instance a boolean) and set it to False to indicate that no duplicate was found

3 Every time you generate a random number, use a For.. Next loop to loop through all the array elements. If any of them is equal to the one you've just generated, change the flag field to True

4 If the flag field is still False after you've gone through the loop, add the generated value to the array.

Important:
Always re-set the flag field before you enter the For.. Next loop, otherwise it might still be True from the previous time.
0

Commented:
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.
0

Commented:
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.
0

Commented:
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 ?
0

Commented:
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
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

0

Commented:
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.
0

Author Commented:
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
0

Author Commented:
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
0

Author Commented:
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
0

Author Commented:
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
0

Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 5
• 4
• 2
Tackle projects and never again get stuck behind a technical roadblock.