Dice Rolling Template

billyshears

Board Regular
Joined
Aug 29, 2013
Messages
61
Does anyone have a template I can download to simulate dice rolling complete with something that looks like Dice? I need something quick and dont have time to build it
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
VBA Code:
Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub DiceRoll()
  Dim i As Long
 
  For i = 1 To 20
    Sleep i ^ 2
    Range("A1") = Int(6 * Rnd() + 1)
    Range("B1") = Int(6 * Rnd() + 1)
    Range("C1") = Int(6 * Rnd() + 1)
    DoEvents
  Next i
End Sub

Doesn't have actual "rolling" dice but the numbers change from fast to slow and then stop.

Download : Roll The Dice Slow Down.xlsm
 
Upvote 0
Here is my solution. It has a little bit of a 'rolling' animation.

The formatting looks weird here on the forum, but if you set up the 'dice' cells as
  • Centered
  • Left Justified
  • Wrap Text
  • Width: 92px
  • Height: 103px

Dice.xlsm
ABCDEF
1● ●● ● ●● ● ● ●● ● ● ● ●● ● ● ● ● ●
2
3
4
5
6
7
8
9● ● ● ● ●● ● ● ● ● ●
10
11
12
13Total: 11
Sheet1
Cell Formulas
RangeFormula
B13B13=MATCH(B9,A1:F1,0)+MATCH(A9,A1:F1,0)


VBA Code:
Sub ROLL()
Dim AR() As Variant: AR = Range("A1:F1").Value
Dim AL As Object: Set AL = CreateObject("System.Collections.ArrayList")
Dim D1 As Integer, D2 As Integer, x As Integer

For i = 1 To 500
    SETAL AR, AL
    setD AL, D1
    setD AL, D2
    Range("A9").Value = AR(1, D1)
    Range("B9").Value = AR(1, D2)
Next i
End Sub

Sub SETAL(AR() As Variant, AL As Object)
For i = 1 To UBound(AR, 2)
    AL.Add i
Next i
End Sub

Sub setD(AL As Object, D As Integer)
Dim x As Integer

x = Int((AL.Count - 1) * Rnd() + 1)
D = AL(x)
AL.Remove x
End Sub
 
Upvote 0
Well, this site is not "excel related", and the dice is 10-sided, while here was a discussion on traditional 6-sided dice.

If one uses Excel2016 or more recent version, UNICHAR function could be used to present standard dice faces.

Adopted @Logit code could be:
VBA Code:
Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub DiceRoll()
  Dim i As Long, j As Long
 
  For i = 1 To 20
    Sleep i ^ 2
    For j = 1 To 3
      Cells(1, j).Value = Evaluate("=UNICHAR(" & Int(6 * Rnd() + 9856) & ")")
    Next j
    DoEvents
  Next i
End Sub

It looks better if one uses rather large font size (dice faces are smaller than standard letters) and horizontal+vertical centering of the cell contents:

1733304208797.png
 
Upvote 0
PS. I thought it would be nice to add some sound while dices are rolling, so:

VBA Code:
Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare PtrSafe Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Sub DiceRoll()
  Dim i As Long, j As Long
  Range("A1:C1").ClearContents
  For j = 1 To 3
    i = sndPlaySound("C:\Users\Kaper\test\roll.wav", CLng(&H3))
    Sleep 250 'adjust numbers to fit the sound
    For i = 9 To 15 'adjust numbers to fit the sound
      Cells(1, j).Value = Evaluate("=UNICHAR(" & Int(6 * Rnd() + 9856) & ")")
      Sleep i ^ 2
    Next i
    DoEvents
  Next j
End Sub
The wav file was downloaded from: http://www.gnubg.org/win32/wavs/roll.wav
Of course folder has to be adjusted in the code (unless you have Users\Kaper\test on your computer :biggrin::biggrin:)
 
Upvote 0

Forum statistics

Threads
1,224,862
Messages
6,181,467
Members
453,045
Latest member
Abraxas_X

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top