For loop / Array Question

Madhadda

New Member
Joined
Oct 11, 2011
Messages
19
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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
Hello Mr. Nate
I always use Option Base 1, which did not appear on my post. Anyway, your piece of code is better.
Talking about code, do you want to write it for Madhadda, or leave it to me?
 
Upvote 0
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.
 
Upvote 0
Well, at the Microsoft MVP page you are Mr. Nathan...(y)

Madhadda:

I'll write it for you tomorrow, because it's 1:15 AM , bed time...:coffee:
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,213,511
Messages
6,114,054
Members
448,543
Latest member
MartinLarkin

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top