Skip to content

Instantly share code, notes, and snippets.

@palikhov
Created February 8, 2022 08:48
Show Gist options
  • Select an option

  • Save palikhov/430fd7d91dbd5f5888210f61cb79147d to your computer and use it in GitHub Desktop.

Select an option

Save palikhov/430fd7d91dbd5f5888210f61cb79147d to your computer and use it in GitHub Desktop.
UDF Roll Dice Formula
Public Function RollDice(r As Range) As Variant
Application.Volatile
Dim v As String, NewForm As String, deemode As Boolean
Dim dee As String
dee = "d"
deemode = False
v = r.Value
NewForm = "="
For i = 1 To Len(v)
ch = Mid(v, i, 1)
If ch = dee Then
NewForm = NewForm & "*RANDBETWEEN(1,"
deemode = True
Else
If Not IsNumeric(ch) And deemode Then
deemode = False
NewForm = NewForm & ")"
End If
NewForm = NewForm & ch
End If
Next i
If deemode Then
NewForm = NewForm & ")"
End If
RollDice = Evaluate(NewForm)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment