Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Sub rolldie()
- On Error GoTo ErrHandler:
- Dim showDebug As Boolean
- showDebug = False
- Dim numRolls As Integer
- Dim currDieCnt As Integer
- Dim currDieRoll As Integer
- Dim numDie As Integer
- Dim dieSize As Integer
- Dim modifier As Integer
- Dim finalRoll As Integer
- Dim inputArray() As String
- Dim debugStr As String
- numRolls = WorksheetFunction.CountA(Selection)
- If numRolls = 0 Then Exit Sub
- For Each currRoll In Selection.Columns(1).Cells
- If InStr(1, LCase(currRoll.Value), "d") = 0 Then GoTo NextIteration
- currDieRoll = 0
- finalRoll = 0
- debugStr = ""
- inputArray = Split(LCase(Replace(currRoll.Value, " ", "")), "d", 2)
- numDie = CInt(inputArray(0))
- If InStr(inputArray(1), "+") > 0 Then
- inputArray = Split(inputArray(1), "+", 2)
- dieSize = CInt(inputArray(0))
- modifier = CInt(inputArray(1))
- Else
- dieSize = inputArray(1)
- modifier = 0
- End If
- For currDieCnt = 1 To numDie
- currDieRoll = CInt(Rnd() * dieSize + 1)
- finalRoll = finalRoll + currDieRoll
- If currDieCnt > 1 Then
- debugStr = debugStr & " + " & CStr(currDieRoll)
- Else
- debugStr = CStr(currDieRoll)
- End If
- Next
- If modifier > 0 Then
- finalRoll = finalRoll + modifier
- debugStr = debugStr & " + " & CStr(modifier)
- End If
- currRoll.Offset(0, 1).Value = CStr(finalRoll)
- If showDebug Then
- currRoll.Offset(0, 2).Value = debugStr
- End If
- NextIteration:
- Next
- Exit Sub
- ErrHandler:
- Set Selection.Offset(0, 1).Value = "Error"
- End Sub
Advertisement