Excecl VBA Classic Excel VBA 2010 Excel VBA 365 Excel VBA Examples About Us

Digital Slot Machine


This is a digital slot machine created using Excel VBA 365. In this example, start MS Excel and click the Developer tab. Click View Code to enter the Visual Basic editor. Insert a UserForm and design the UI for the slot machine. Insert three labels for displaying the digits, one label to display the "Slot Machine " name, two command buttons which is used for spinning and ending the program.

We use the Rnd function to generate random numbers from 1 to 9 using the formula Int(Rnd * 9)+1 that will be shown on the three labels.

We use the Do..Loop procedure to generate the random numbers and pause it momentarily between each loop using the DoEvents command. This will create the animation effect, simulating a spinning slotmachine. DoEvents is an Excel VBA command that temporarily pauses the execution of the macro to refresh the screen and execute any pending events in Excel.

We use the If...Then..ElseIf statements to check for JackPot and Mini Jackpots. If all numbers are 7 then it will be the Jackpot, if any two same numbers appear then it will be a mini jackpot.

The Code

Private Sub CommandButton1_Click()
Dim x As Integer

x = 0
Do

x = x + 2

 Label1.Caption = Int(Rnd * 9) + 1
 Label2.Caption = Int(Rnd * 9) + 1
 Label3.Caption = Int(Rnd * 9) + 1
 
DoEvents

Loop Until x = 10000
 If (Label1.Caption = 7) And (Label2.Caption = 7) And (Label3.Caption = 7) Then
 MsgBox ("Your strike the Jackpot")
 ElseIf (Label1.Caption = Label2.Caption) Or (Label1.Caption = Label3.Caption) Or (Label2.Caption = Label3.Caption) Then
  MsgBox ("Your strike the Mini Jackpot")
 End If
 
End Sub

 
Figure 25.2 The Design Interface
Figure 25.3 The Runtime Interface
Figure 25.4 Mini Jackpot Dialogue





Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page