# For loop / Array Question

##### New Member
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.

### Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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``````

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``````

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.

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

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.

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...

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...

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.

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.

Last edited:

Replies
5
Views
215
Replies
5
Views
139
Replies
2
Views
261
Replies
12
Views
177
Replies
0
Views
270

1,203,525
Messages
6,055,916
Members
444,834
Latest member
ComputerExcel

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back