A
LOTTERY GAME IN EXCEL  GENERATE RANDOM NUMBERS


Home  
Excel 2007  Excel 2003  Macros Excel 2007  Macros Excel 2003  Excel 2010 





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 (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: 



Home  
Excel 2007  Excel 2003  Macros Excel 2007  Macros Excel 2003  Excel 2010 
