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.
Thanks for your help in advance!

A VB For loop looks like this:

For i = 1 to 5
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
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

    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. http://www./

      perfectly, submit a website link or explain to us how you can watch the clip. I tried but did not come across anything at all on web site. BTW- do you blame romney as he’s cautious following the flap on the cairo stuff earlier this month?

  2. sparda bank kreditkartenabrechnung

    I would like to thnkx for the efforts you’ve put in writing this blog. I am hoping the same high-grade web site post from you in the upcoming as well. Actually your creative writing abilities has inspired me to get my own blog now. Really the blogging is spreading its wings fast. Your write up is a great example of it.


Leave a Reply

Your email address will not be published. Required fields are marked *