【问题标题】:Running counter each time a function is executed (VBA)每次执行函数时运行计数器(VBA)
【发布时间】:2017-09-20 04:51:06
【问题描述】:

我对数组的索引有点挣扎。我希望数组的上限是函数 RandomizeDice 执行的时间量。任何帮助是极大的赞赏。

Function RandomizeDice()
    RandomizeDice = Application.WorksheetFunction.RandBetween(1, 6)
End Function

Sub RollDice()
    Dim DiceOne() As Variant
    Dim DiceTwo() As Variant
    Dim SumDice() As Variant
    Dim i As Integer
    ReDim DiceOne(i) As Variant
    ReDim DiceTwo(i) As Variant
    ReDim SumDice(i) As Variant
    Call arraySet(DiceOne(), DiceTwo(), SumDice())
    Debug.Print SumDice(i)
    'Debug.Print SumDice(0)
   ' Debug.Print ("Dice: " & DiceOne(0) & " " & DiceTwo(0))
   ' Debug.Print ("Sum: " & DiceOne(0) + DiceTwo(0))
End Sub
Sub arraySet(ByRef a() As Variant, b() As Variant, c() As Variant)
    'Dim DiceOne() As Integer
    'Dim DiceTwo() As Integer
    Dim i, j As Integer
    'Dim intSumDice() As Integer
    For i = 0 To j = i + 1
        a(i) = RandomizeDice() 'dice1
        b(i) = RandomizeDice() 'dice2
        c(i) = a(i) + b(i) 'sum
    Next i
    Debug.Print i
    Debug.Print ("Dice: " & a(0) & " " & b(0))
    Debug.Print ("Sum: " & a(0) + b(0))
End Sub

【问题讨论】:

  • @Joel Coehoorn 你真的帮了我关于这个主题的最后一篇文章,所以你可能提供了一些见解?

标签: arrays vba excel function


【解决方案1】:

使您的RollDice 以滚动数为参数

Sub RollDice(ByVal nRolls As Long)
    ReDim DiceOne(1 To nRolls) As Long, DiceTwo(1 To nRolls) As Long, SumDice(1 To nRolls) As Long
    For nRolls = 1 To nRolls
        DiceOne(nRolls) = RandomizeDice()
        DiceTwo(nRolls) = RandomizeDice()
        SumDice(nRolls) = DiceOne(nRolls) + DiceTwo(nRolls)
    Next
    ' Now do what you want with these arrays
End Sub

Sub testing()
    RollDice 100
End Sub

【讨论】:

    【解决方案2】:

    您可以使用静态变量来实现这一点。静态变量的值在函数调用之间保持不变。请参阅下面的解决方案。运行下面代码中的Sub Main 几次,看看效果如何。

    Option Explicit
    
    Public Type structPairOfDice
        diceOne As Integer
        diceTwo As Integer
        rollNum As Integer
    End Type
    
    Function RandomizeDice() As Integer
        RandomizeDice = Application.WorksheetFunction.RandBetween(1, 6)
    End Function
    
    Function RollDice(structDice As structPairOfDice) As structPairOfDice
    
        Static iRollNum As Integer
    
        iRollNum = iRollNum + 1
        With structDice
            .rollNum = iRollNum
            .diceOne = RandomizeDice()
            .diceTwo = RandomizeDice()
        End With
    
        RollDice = structDice
    
    End Function
    
    Sub PrintResults(structDice As structPairOfDice)
    
        With structDice
            Debug.Print "Roll #: " & .rollNum
            Debug.Print "Dice:   " & .diceOne & ", " & .diceTwo
            Debug.Print "Sum:    " & .diceOne + .diceTwo
            Debug.Print
        End With
    
    End Sub
    
    Sub Main()
    
        Dim structDice As structPairOfDice
    
        PrintResults RollDice(structDice)
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 2012-04-26
      • 2021-12-03
      相关资源
      最近更新 更多