如何找到 VBA 内而不是 excel 范围内的最小数组

How to find the smallest of an array within VBA, not in an excel range

这里的基本思路是投掷 4 d6,掉落最低。

For lngPosition = LBound(strAttributes) To UBound(strAttributes)

        i = 0
        For shortPosition = LBound(intRoll) To UBound(intRoll)
           intRoll(shortPosition) = Int((6 - 1 + 1) * Rnd + 1)
           i = intRoll(shortPosition) + i
        Next shortPosition

        i = {i - smallest intRoll()}

        strAttributes(lngPosition) = i

    Next lngPosition

我找到了很多关于如何通过在定义范围后添加 .Small 来找到 excel 范围内的最低值的信息,但我认为这行不通这里。可能是我对如何实现这一点有些迷茫,但我太新鲜了,我真的不知道。

谢谢

假设你的数组是一维数组,你仍然可以使用工作表函数class,比如:

Dim myArray
myArray = Array(5, 3, 103, 99, -14, 12)

Debug.Print Application.WorksheetFunction.Small(myArray, 1)

如果您的数组是多维的,那么您将需要使用强力迭代。

这是一个如何实现它的例子:

Sub test()
    Dim a(10), i&
    For i = 0 To 10
       a(i) = i
    Next
    MsgBox "Min: " & WorksheetFunction.Min(a) & Chr(10) & _
            "Avg: " & WorksheetFunction.Average(a) & Chr(10) & _
            "Max: " & WorksheetFunction.Max(a)
End Sub