-
Solved: Display Animation While Random Numbers procedure
hi..
i'm new here and i just want to know if anyone can help me to animate my random number generator... i have a a user form with a textbox that display the result... i just want it to look like a slot machine before it will display the final result...
thanks
-
Knowledge Base Approver
The King of Overkill!
VBAX Master
Hi,
Welcome to VBAX
Take a look at the attachment, a quick slot machine userform I just made which should give you what you're looking to do.
Matt
-
thanks matt you really are great i want to make my file like the one you did but i don't know where to put it and how... this is my code so please help me update it
thanks
[VBA]
Private Sub UserForm_Initialize()
Call RemoveCaption(Me)
End Sub
Private Sub Workbook_Open()
Call timer
End Sub
Sub timer()
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
TextBox4 = ""
Dim x As String, i As Byte
Randomize
x = Format$(Int(Rnd * 1899) + 1, "0000")
For i = 1 To Len(x)
Application.Wait Now + TimeValue("00:00:03")
Me.Controls("TextBox" & i).Value = Mid$(x, i, 1)
Next
Dim vTime As Date
vTime = Now
Do Until Now > vTime + TimeValue("00:00:03") 'spin all 3 for 3 seconds
Show TextBox1.Value
Show TextBox2.Value
Show TextBox3.Value
Show TextBox4.Value
Loop
End Sub
Private Sub CommandButton1_Click()
timer
End Sub
Private Sub Label2_Click()
End Sub
Private Sub TextBox1_Change()
Sheet1.Cells(Rows.Count, "A").End(xlUp)(2, 1) = TextBox1.Value
End Sub
Private Sub TextBox2_Change()
Sheet1.Cells(Rows.Count, "B").End(xlUp)(2, 1) = TextBox2.Value
End Sub
Private Sub TextBox3_Change()
Sheet1.Cells(Rows.Count, "C").End(xlUp)(2, 1) = TextBox3.Value
End Sub
Private Sub TextBox4_Change()
Sheet1.Cells(Rows.Count, "D").End(xlUp)(2, 1) = TextBox4.Value
End Sub
Private Sub UserForm_Click()
End Sub
[/VBA]
-
-
Knowledge Base Approver
The King of Overkill!
VBAX Master
Sure First off, for future reference, you may want to unlock the VBA for any attachment you put up here for help with. Luckily I can still get in them, but most people wouldn't know how (and we don't show how either).
If I read your code correctly, you want a one-digit number in each text box? It looks like textbox1 could be a 0 or 1, and textbox2/textbox3/textbox4 could be 0-9 (with textbox2 having less of a chance to be a 9 than the textbox3 or textbox4). If that is not your intent, please explain what you're looking for exactly and I can edit the code to suit you.
I don't really see a reason for your _Change events, so I removed them. I'm guessing you want the random numbers transferred to the worksheet, so I put that code in the event that gets triggered when the userform is closed.
Also, you may want to change the textboxes to labels, so the user can't change them manually (you'll have to change TextBoxName.Text to LabelName.Caption in the code if you do).
All that being said, replace the code in your userform with the following:[vba]Option Explicit
Sub StartSpin()
Dim vTime As Date
vTime = Now
Do Until Now > vTime + TimeValue("00:00:03")
ShowRandomIn1
ShowRandomIn2
ShowRandomIn3
ShowRandomIn4
Loop
vTime = Now
Do Until Now > vTime + TimeValue("00:00:01")
ShowRandomIn2
ShowRandomIn3
ShowRandomIn4
Loop
vTime = Now
Do Until Now > vTime + TimeValue("00:00:01")
ShowRandomIn3
ShowRandomIn4
Loop
vTime = Now
Do Until Now > vTime + TimeValue("00:00:01")
ShowRandomIn4
Loop
End Sub
Sub ShowRandomIn1()
Randomize
TextBox1.Text = CStr(Int(Rnd * 2))
DoEvents
End Sub
Sub ShowRandomIn2()
Randomize
TextBox2.Text = Mid$(Format(Int(Rnd() * 1900), "0000"), 2, 1)
DoEvents
End Sub
Sub ShowRandomIn3()
Randomize
TextBox3.Text = CStr(Int(Rnd * 10))
DoEvents
End Sub
Sub ShowRandomIn4()
Randomize
TextBox4.Text = CStr(Int(Rnd * 10))
DoEvents
End Sub
Private Sub CommandButton1_Click()
StartSpin
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Sheet1.Cells(Rows.Count, "A").End(xlUp)(2, 1) = TextBox1.Text
Sheet1.Cells(Rows.Count, "B").End(xlUp)(2, 1) = TextBox2.Text
Sheet1.Cells(Rows.Count, "C").End(xlUp)(2, 1) = TextBox3.Text
Sheet1.Cells(Rows.Count, "D").End(xlUp)(2, 1) = TextBox4.Text
End Sub[/vba]
Matt
-
again thank you... what i want to do is generate a random number from 1 to 1899 and like a slot machine i want it 1 figure at a time so i created 4 textboxs and keep the generated number in the worksheet for future reference...
so please help me
-
Knowledge Base Approver
The King of Overkill!
VBAX Master
Ok, I understand now (I was going about it strangely anyways).
Change your userform code to:[vba]Option Explicit
Private Sub CommandButton1_Click()
Dim vTime As Date, vNumber As String, i As Long, j As Long, k As Long
'pick the number first
Randomize
vNumber = Format(Int(Rnd * 1899 + 1), "0000")
'now animate the boxes just for show
For i = 1 To 4
vTime = Now
Do Until Now > vTime + TimeValue("00:00:01")
Randomize
If i = 1 Then
TextBox1.Text = CStr(Int(Rnd * 2))
k = 2
Else
k = i
End If
For j = k To 4
Me.Controls("TextBox" & CStr(j)).Text = CStr(Int(Rnd * 10))
Next
DoEvents
Loop
Me.Controls("TextBox" & CStr(i)).Text = Mid(vNumber, i, 1)
Next
'transfer values to worksheet
Sheet1.Cells(Rows.Count, "A").End(xlUp)(2, 1) = TextBox1.Text
Sheet1.Cells(Rows.Count, "B").End(xlUp)(2, 1) = TextBox2.Text
Sheet1.Cells(Rows.Count, "C").End(xlUp)(2, 1) = TextBox3.Text
Sheet1.Cells(Rows.Count, "D").End(xlUp)(2, 1) = TextBox4.Text
End Sub[/vba]That should do it!
Matt
-
Knowledge Base Approver
The King of Overkill!
VBAX Master
Also, you do not need to send me a private message to tell me you've posted a comment. I get the notification emails from this site that tell me when the thread is updated anyways, and I will always come back to it when I can. Sending me a PM doesn't make me go any faster
Matt
-
-
Knowledge Base Approver
The King of Overkill!
VBAX Master
I'm happy to help, I just wanted to make sure you knew it wasn't necessary
Let me know if you need anything else with it
Also, you can mark this solved by going to Thread Tools at the top then Mark Thread Solved
Matt
-
Knowledge Base Approver
The King of Overkill!
VBAX Master
And just for fun I've rewritten it as a shorter, yet still readable, version [vba]Private Sub CommandButton1_Click()
Dim vTime As Date, vNumber As String, i As Long, j As Long, vTemp As String
'pick the number first
Randomize
vNumber = Format(Int(Rnd * 1899 + 1), "0000")
'now animate the boxes just for show
For i = 1 To 4
vTime = Now
Do Until Now > vTime + TimeValue("00:00:01")
vTemp = Format(Int(Rnd * 1899 + 1), "0000")
For j = i To 4
Me.Controls("TextBox" & CStr(j)).Text = Mid$(vTemp, j, 1)
Next
DoEvents
Loop
Me.Controls("TextBox" & CStr(i)).Text = Mid$(vNumber, i, 1)
Next
'transfer values to worksheet
Sheet1.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(1, 4).Value = _
Array(TextBox1.Text, TextBox2, TextBox3.Text, TextBox4.Text)
End Sub[/vba]
Matt
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules