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.
 
Coerced Byte Arrays will always give you a 0-based Array, too.
Yes, I do remember that now that you mention it. I use the Split function quite a lot whereas I almost never use Byte arrays which is why the Split function came to mind and Byte arrays didn't. Thanks for following up with that.

I stick with defining/testing my boundaries.
Agreed... that is definitely the better way to make use of declared arrays.
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
As promised... I didn' t use strings at all, if you need them for some reason please say so.
Check if the routine does what you want.

Code:
Option Explicit

Sub Lottery()
Dim UserArray&(), winner&, ncards%, i%, rich As Boolean

Randomize
rich = False
winner = Round(10000 + Rnd() * 89999)
ncards = Application.InputBox(prompt:="How many cards?", Type:=1)
ReDim UserArray(1 To ncards)
For i = 1 To ncards             ' fills the array
    UserArray(i) = Round(10000 + Rnd() * 89999)
Next
i = 1

Do
    If UserArray(i) = winner Then rich = True
    i = i + 1
Loop Until (rich Or i = (ncards + 1))

If rich Then
    MsgBox "Congratulations !!!", vbExclamation, "Winning number= " & winner
    
Else
    MsgBox "Buy more tickets..."
End If
End Sub
 
Upvote 0
This will work perfectly, thanks for taking your time to help me out man, (y) cheers.

As promised... I didn' t use strings at all, if you need them for some reason please say so.
Check if the routine does what you want.

Code:
Option Explicit

Sub Lottery()
Dim UserArray&(), winner&, ncards%, i%, rich As Boolean

Randomize
rich = False
winner = Round(10000 + Rnd() * 89999)
ncards = Application.InputBox(prompt:="How many cards?", Type:=1)
ReDim UserArray(1 To ncards)
For i = 1 To ncards             ' fills the array
    UserArray(i) = Round(10000 + Rnd() * 89999)
Next
i = 1

Do
    If UserArray(i) = winner Then rich = True
    i = i + 1
Loop Until (rich Or i = (ncards + 1))

If rich Then
    MsgBox "Congratulations !!!", vbExclamation, "Winning number= " & winner
    
Else
    MsgBox "Buy more tickets..."
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,131
Messages
6,123,223
Members
449,091
Latest member
jeremy_bp001

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