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

Professional Slot Machine


We shall create slot machine that resembles the real slot machines in the casino, using Excel VBA 365(Older versions should be able to work too).

To create the project, start an Excel 365 workbook and save the file as slotmachine.xlsm or some other file name you like. Click the Developer tab and enter the Visual Basic Editor. In the VBE, insert a UserForm and design the user interface, as shown in the figure below:

The Slotmachine Design UI

First, insert three image boxes and load some pictures into them, we used the images of a flower, an orange and an ice cream. We shall program them to appear randomly when the user presses on the spin button, resembling a slot machine.

In addition, we insert a few labels and command buttons and a text box. Label one command as Spin , another one as Add Cash and the last one as Cash Out. Besides that, one label is to display the balance and the other one is to display the winning amount of each spin.

We shall program the slot machine as a 20¢ machine, where each spin costs 20¢. We shall also determine how much money won for different combinations.

We will create a sub procedure and name is as spin(). This code for this sub procedure uses a randomize process to generate different combinations of the images. Besides that, we use a Do Loop and the DoEvents command to create an animation effect. The code for the Spin() sub procedure is:

The Code for Spin()

Sub spin()
Do

Lbl_Message.Visible = False
Txt_Amount.Text = ""

a = 1 + Int(Rnd * 3)
b = 1 + Int(Rnd * 3)
c = 1 + Int(Rnd * 3)

If a = 1 Then
Image1.Picture = LoadPicture("C:\Users\LENOVO\Documents\My Websites\Excelvbatutor\ExcelVBA\ExcelVBAExamples\images\orange.gif")
End If

If a = 2 Then
Image1.Picture = LoadPicture("C:\Users\LENOVO\Documents\My Websites\Excelvbatutor\ExcelVBA\ExcelVBAExamples\images\flower.gif")
End If

If a = 3 Then
Image1.Picture = LoadPicture("C:\Users\LENOVO\Documents\My Websites\Excelvbatutor\ExcelVBA\ExcelVBAExamples\images\icecream.gif")
End If

If b = 1 Then
Image2.Picture = LoadPicture("C:\Users\LENOVO\Documents\My Websites\Excelvbatutor\ExcelVBA\ExcelVBAExamples\images\orange.gif")
End If

If b = 2 Then
Image2.Picture = LoadPicture("C:\Users\LENOVO\Documents\My Websites\Excelvbatutor\ExcelVBA\ExcelVBAExamples\images\flower.gif")
End If

If b = 3 Then
Image2.Picture = LoadPicture("C:\Users\LENOVO\Documents\My Websites\Excelvbatutor\ExcelVBA\ExcelVBAExamples\images\icecream.gif")
End If

If c = 1 Then
Image3.Picture = LoadPicture("C:\Users\LENOVO\Documents\My Websites\Excelvbatutor\ExcelVBA\ExcelVBAExamples\images\orange.gif")
End If

If c = 2 Then
Image3.Picture = LoadPicture("C:\Users\LENOVO\Documents\My Websites\Excelvbatutor\ExcelVBA\ExcelVBAExamples\images\flower.gif")
End If

If c = 3 Then
Image3.Picture = LoadPicture("C:\Users\LENOVO\Documents\My Websites\Excelvbatutor\ExcelVBA\ExcelVBAExamples\images\icecream.gif")
End If

If (a <> b And b <> c And c <> a) Then
     win = 0
  End If
 If (a = 1 And b = 1 And c <> 1) Or (a = 1 And c = 1 And b <> 1) Or (b = 1 And c = 1 And a <> 1) Then
     win = 5
  End If

If (a = 2 And b = 2 And c <> 2) Or (a = 2 And c = 2 And b <> 2) Or (b = 2 And c = 2 And a <> 2) Then
   win = 10
 End If
 
 If (a = 3 And b = 3 And c <> 3) Or (a = 3 And c = 3 And b <> 3) Or (b = 3 And c = 3 And a <> 3) Then
 win = 20
 End If
 If (a = 1 And b = 1 And c = 1) Then
 win = 100
 End If
 
If (a = 2 And b = 2 And c = 2) Then
 win = 150
 End If
If (a = 3 And b = 3 And c = 3) Then
  
  win = 200
 End If
 x = x + 2
 DoEvents
 
 Loop Until x = 1000
 Lbl_Win.Caption = Str(win)
 If win = 100 Then
 
 Lbl_Message.Visible = True
 Lbl_Message.Caption = " Mini Jackpot!"
End If
If win = 150 Then
 
 Lbl_Message.Visible = True
 Lbl_Message.Caption = "Jackpot!"
End If
If win = 200 Then
 
 Lbl_Message.Visible = True
 Lbl_Message.Caption = "Mega Jackpot!"
End If
   
updatebalance

End Sub

We also need to program the Spin command button to call the Spin() sub procedure. The code is as follows:

Private Sub Cmd_Spin_Click()
Dim x, a, b, c, win As Integer
If Val(Lbl_Balance.Caption) < 20 Then 'The user cannot spin when the balance falls below 20
MsgBox ("Please add credit")  'Prompts the user to add cash when the balance falls below 20
Txt_Amount.SetFocus
Else
spin
End If

In addition, we need to create a sub procedure to keep track of the balance. We use the static keyword to create the sub procedure to prserve the value of the balance amount. The code is as follows:

Static Sub updatebalance() 
Dim myamount, balance As Integer

myamount = Val(Lbl_Win.Caption) - 20
balance = Val(Lbl_Balance.Caption) + myamount
Lbl_Balance.Caption = Str(balance)

End Sub
The Slotmachine Runtime UI
Mega Jackpot






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

Contact: Facebook Page