Question:
Microsoft Excel VBA Loops????? helppppp plzzzzzzzzzzzzzz?
xtreme1131
2009-03-07 16:40:46 UTC
Hi, i have to create a lottery game in excel using vba.

i have 4 numbers going cross from C3 to F3

i have to make a button that when clicked, each number in the cells i listed above change randomly. This button i have to make using sequential VBA instructions. In other words, with no "for loop" structure.

The second button must have the same function, but this one has the "for loop" structure

The last button is the same but this one generates 4 random letters instead, in four different specific cells. This one also uses the "for loop" structure.

If anyone could jus explain what the "for loop" structure is, and how to use it, that would be great. Also, if you know what the sequential VBA instructions are for random numbers without the for loop structure, that would also be great.

Any Help is appreciated, thanks.
Three answers:
ArmchairPilot
2009-03-07 18:46:48 UTC
A VB For loop looks like



For i = 1 to 10

(statements)

Next i



Where i is called the loop index, or loop variable or loop counter: the group of statements will be executed 10 times, and each time through the loop is called a pass.



The loop variable is often used in statements where one value changes each pass though the loop, and the loop variable is often fashioned to suit conditions more closely



For instance in your application, the cells change form column3 to column 6 while the row remains constant (=3)



To place the column value in each of the cells using a loop: note the range of the index from 3 to 6



For col=3 to 6

Cells(3,col)=col    ' output goes to spread sheet cell row 3, col3 first ie C3

Next col



The random function in VBA is called Rnd



The function generates a "random" number between 0 and 0.9999999...



So to generate a (pseudo-)random sequence of numbers there has to be an upper and lower limit



Just RN = Rnd(10) gives a number between 0 and 9.99999



Most programmers will explicitly remove the decimal portion with the Int function



Between 1 and 10: RN = Int(Rnd(10)) + 1

Between 20 and 30: RN + Int(Rnd(11)) + 10

Between Upper an Lower: RN = Int(Rnd(Upper-Lowe+1))+1



Each time you run a program, the same random sequence will re-appear: to make it different each time, add the statement



Randomize



at the beginning of your program
lucie m
2009-03-07 16:53:33 UTC
A For loop is used when you want to execute a line of code for a set number of times.



Dim Counter As Integer 'declares a variable called Counter

For Counter = 1 to 5 ' starts loop to execute 5 times

Print "hello" 'instruction to be executed 5 times

Next Counter ' ends loop



The result of this would be

hello

hello

hello

hello

hello



hope this helps :)
anonymous
2016-10-25 03:30:56 UTC
You do have a "Do" - it really is on-line 3: "Do till y = 28" i'm no longer confident in the experience that your code's formatting become tangled up even as it become pasted into yahoo, yet you want to regulate all of your "Else If" instructions to "ElseIf" so as that the If fact will be finished. there's a distinction between both. contained in the tactic that you've listed on your question, the If turns into nested contained in the better element If, so that you're shifting to extra layers of situations and under no circumstances resolving all the If/Then statements. Your code must be rewritten like this: x = 7 y = 22 Do till y = 28 Worksheets("Mod").Cells(x, a million).FormulaR1C1 = Worksheets("GA-a million").Cells(y, a million).FormulaR1C1 Worksheets("Mod").Cells(x, 2).FormulaR1C1 = Worksheets("GA-a million").Cells(y, 2).FormulaR1C1 If Worksheets("GA-a million").Cells(y, 4).fee <> 0 Then Worksheets("Mod").Cells(x, 8).fee = Worksheets("GA-a million").Cells(y, 4).fee x = x + a million ElseIf Worksheets("GA-a million").Cells(y, 5).fee <> 0 Then Worksheets("Mod").Cells(x, 8).fee = Worksheets("GA-a million").Cells(y, 5).fee x = x + a million ElseIf Worksheets("GA-a million").Cells(y, 6).fee <> 0 Then Worksheets("Mod").Cells(x, 8).fee = Worksheets("GA-a million").Cells(y, 6).fee x = x + a million end If y = y + a million Loop


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...