I am currently trying to create a program which takes a variable number of players (denoted by (NumPlayerText.Value - 1)) and has them roll a die (NumThrowText.Value - 1) times. The values for each players rolls will be summed and compared, with the highest winning.
During the function, a loop designed to simulate (NumThrowText-1) dice rolls and add the values malfunctions ONLY when the number of players is higher than the number of rolls.
Here's the two interacting functions.
Function rollsumvalue(playernum)
Dim rolls()
ReDim rolls(NumThrowText.Value - 1)
ReDim rollsum(NumPlayerText.Value - 1)
For playernum = 0 To (NumThrowText.Value - 1)
rolls(playernum) = rolls(playernum) + random_generator(1, 6)
Next
rollsum(playernum) = (WorksheetFunction.Sum(rolls(playernum)))
End Function
Private Function callrollsumvalue()
For player = 0 To (NumPlayerText.Value - 1)
rollsumvalue (player)
MsgBox ("Rolled a total " & rollsum(player) & " points")
Next
End Function
rollsum is a global variable different than rolls FYI.
Debug shows subscript out of range from
rolls(playernum) = rolls(playernum) + random_generator(1, 6)
However I cannot see why this occurs only when # of players > # of rolls
Sorry for the long question. Any help is appreciated!
Debug.Print NumThrowText.Value - 1)return? What is the value ofplayernum?redim rollsum(NumPlayerText.Value - 1)); like this it doesn't make sense to have it as a global variable. Furthermore you are dimming rolls based onNumThrowText.Value - 1but use playernum as index for adding the random value. I think it would be sufficient if you have roll as a long-variable ..Subs. Also "rollsum is a global variable"... red flag.