# Lottery Game in Excel

A Lottery Game in MS Excel by Generating Random Numbers
Hi, i have to create a lottery game in excel using vba.
i have 5 numbers going cross from C2 to G2
i have to make a button that when clicked, each number in the cells i listed above change randomly.
If you could just explain what the “for loop” structure is, and how to use it, that would be great.

A VB For loop looks like this:

For i = 1 to 5
(statements)
Next i

Here i is called loop counter: the statements will be executed 5 times, and each time through the loop is called a pass i. e. if the statement looks like this:
print “Hello”
then the code will print “Hello” 5 times.

During the first pass the loop counter or variable i will be equal to 1. In the next pass it will be 2 and so on…

In the case of your problem we’ll need to change the column numbers and keep the row constant since the values of the random number will be displayed in columns C2 to G2. So the columns change from C to D to E to …. G. The row 2 remains constant.
So our ‘for loop’ structure using the cells property will look like this:

For i = 3 To 7
RN = (“=RandBetween(1, 99)”)
Cells(2, i) = RN
Next i

While the loop is in progress we’ll also generate a random number between 1 and 99 using the ‘RANDBETWEEN()’ function and assign the generated number to an appropriate column. Therefore, when the value of
cells(2,i)=cells(2,3)
where the first number 2 represents the row and the second number 3 represents the column, the random number RN generated will be placed in cell C2 and accordingly as the loop progresses. Once the value of i reaches 7 or cell G2 the ‘for loop’ will be terminated since the next value of i will be 8 or cell H2 and we have coded that the loop run only upto i=7.
Also we have attached the above code to a command button. On clicking the command button every time a new set of random numbers is generated and your lottery game is ready.
Now take a pen and write down your 5 lucky numbers between 1 and 99 before you click on your command button and check to see if you have won. Invite your friends too!
Watch the video below (~9.5 MB) to learn the implementation of the lottery game in action:

## 6 thoughts on “Lottery Game in Excel”

1. Sandeep Gupta says:

Being Inspired from your videos, I have learned a lot of things from your websites and video tutorials:

Please look on this code which i have modified.

Sub T23_Lottery_Game()
For i = 3 To 7
Cells(4, i) = WorksheetFunction.RandBetween(70, 90)
Next i
End Sub

1. Hey! This is kind of off topic but I need some advice from an established blog. Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about creating my own but I’m not sure where to begin. Do you have any points or suggestions? Appreciate it

2. Hej Anna.Jeg vil gerne give 150 eller lidt til for den sags skyld.Jeg har ledt og ledt efter en ny jakke med lidt stil og farve, sÃ¥ det er virkelig en jakke som jeg har hÃ¥rdt brug for :)Har du et billede med dig der bÃ¦re den, sÃ¥ vil jeg gerne se det:)

3. thot I’d come over and check out your house. Dude, not much has changed in the white liberal world since roughly forever. All good intentions. The same ones which pave the road to you know where. Just wanted to say the second part of your “Four White males” essay on MLW was narrative art. That voice of yours is something special. I didnt want to Leonard Bernstein you with repeated loveletters at the white liberal cocktail party but here I will state it again: excellent writing.

4. raghu prabhu says:

Hi Dinesh,

this does not work…

Private Sub CommandButton1_Click()
Dim i As Long
Dim RN As Long
For i = 3 To 7
RN = (“=RandBetween(1, 99)”)
Cells(2, i) = RN
Next i
End Sub

says data mismatch…