将随机骰子的值放入一个数组(VBA)

我想弄清楚如何创build三个给定的函数返回值的数组。 我不把数据放到任何地方的Excel,所以我不能使用“范围”。 我相信有一个解决scheme,但我还没有弄清楚。

'randomize first dice Function DiceOne(ByRef intDiceOne As Integer) intDiceOne = Application.WorksheetFunction.RandBetween(1, 6) DiceOne = intDiceOne End Function 'randomize second dice Function DiceTwo(ByRef intDiceTwo As Integer) intDiceTwo = Application.WorksheetFunction.RandBetween(1, 6) DiceTwo = intDiceTwo End Function 'add first and second dice Function RollDice() Dim intDiceOne As Integer Dim intDiceTwo As Integer Dim intSumDice As Integer Dim i As Integer DiceOne intDiceOne DiceTwo intDiceTwo intSumDice = intDiceOne + intDiceTwo RollDice = intSumDice 'Application.Range("dice_one") = intDiceOne 'Application.Range("dice_two") = intDiceTwo 'Application.Range("dice_sum") = intSumDice 'Debug.Print "The roll value is " & intSumDice End Function 

你不需要为每个死亡单独的function,而且你不需要ByRef 。 做这个:

 Function RollD6() RollD6 = Application.WorksheetFunction.RandBetween(1, 6) End Function Sub RollDice() Dim Dice(2) As Integer For i = 0 To 2 Dice(i) = RollD6() Next Debug.Print("Dice: " & Dice(0) & " " & Dice(1) & " " & Dice(2)) Debug.Print("Sum: " & Dice(0) + Dice(1) + Dice(2)) End Sub