# Thread: For loop / Array Question Thanks: 0 Likes: 0

1. ## For loop / Array Question

So i'm supposed to generate a 5 digit number which i will compare to other numbers. I should then store it into a string variable:

i = Rnd() * (99999 - 9999) + 9999
number = LTrim(str(i))

Then im supposed to ask how many numbers a person would like to generate to try to match to this number I already created. I am to use a for loop and store these numbers into a string array (Which im not sure how to do). Ill finish it up with a do loop that trys to match these numbers with the winning number up top. My problem is, im not sure how to generate the amount the user wants.
I.E. how do I structure a For loop to generate the amount of numbers a person wants, and then store it into a string array. Thanks for reading.

2. ## Re: For loop / Array Question

Hi
Like Jack the Ripper, let's do it part-wise...

Is this the kind of string array you are after?
Code:
```Sub EArray()
Dim strArr\$(5), msgvar\$, i%, num#
msgvar = " "
For i = 1 To 5
num = Rnd()
strArr(i) = CStr(num)
msgvar = msgvar & strArr(i) & "  "
Next
MsgBox msgvar
End Sub```

3. ## Re: For loop / Array Question

A small comment, your Array actually has six elements, so it might make sense to declare it as 1-based, e.g.,

Code:
```Sub EArray()
Dim strArr(1 To 5) As String, msgVar As String
Dim i As Long, num As Double
MsgBox LBound(strArr)
For i = 1 To 5
Let num = Rnd()
Let strArr(i) = num
Let msgVar = Join\$(strArr, " ")
Next
MsgBox msgVar
End Sub```

4. ## Re: For loop / Array Question

Thanks for the reply guys! ive run what youve given me and think I may have confused you as to what im looking for, Im a little slow with this stuff so bear with me.

Im asking for user input, for example, how many cards they would like to buy.
For each card the user wants to buy, i need to generate a 5 digit number. Then, with all these numbers stored in an array, I will see if they match the first winning 5 digit number ive already declared in the code.

I know this will require a for loop I just dont know how to structure it. Thanks.

5. ## Re: For loop / Array Question

Hello Mr. Nate
I always use Option Base 1, which did not appear on my post. Anyway, your piece of code is better.

6. ## Re: For loop / Array Question

Hopefully its not too time consuming, Its not too tough im just not sure how to set it up, any advice is appreciated, thanks for the help so far guys.

7. ## Re: For loop / Array Question

Please don't call me Mr., Nate is all good, I didn't mean to be rude. Go ahead and write it for him - although I'm not sure why, it's pretty easy.

Its not too tough im just not sure...

8. ## Re: For loop / Array Question

Well, at the Microsoft MVP page you are Mr. Nathan...

I'll write it for you tomorrow, because it's 1:15 AM , bed time...

9. ## Re: For loop / Array Question

Originally Posted by Worf
I always use Option Base 1
Just a word of caution when/if you use the Split function... it always returns a zero-based array no matter what your Option Base setting is.

10. ## Re: For loop / Array Question

Coerced Byte Arrays will always give you a 0-based Array, too. E.g.,

Code:
```Option Explicit
Option Base 1

Sub foo()
Dim b() As Byte
Let b = "Hello World"
MsgBox LBound(b)
End Sub```
I don't trust the Option Base setting, personally - I stick with defining/testing my boundaries.

In terms of my profile... That wasn't up to me. Let's just stick with Nate.

## User Tag List

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•