Solved

Putting values into an array?

Posted on 1999-01-14
11
181 Views
Last Modified: 2010-05-03
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
Comment
Question by:sheets1
  • 5
  • 4
  • 2
11 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 30 total points
ID: 1456162
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456163
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
 
LVL 2

Expert Comment

by:cartti
ID: 1456164
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
 
LVL 2

Expert Comment

by:cartti
ID: 1456165
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456166
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

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Expert Comment

by:caraf_g
ID: 1456167
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 Comment

by:sheets1
ID: 1456168
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 Comment

by:sheets1
ID: 1456169
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 Comment

by:sheets1
ID: 1456170
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 Comment

by:sheets1
ID: 1456171
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456172
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now