如何在VBA中find最小的数组,而不是在excel范围内

这里基本的想法是roll 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 

我已经find了很多关于如何在excel中find范围中最低的值的信息,在定义范围之后添加.Small ,但是我不认为这是可行的。 这可能是因为我如何做到这一点,但我很新鲜,我真的不知道。

谢谢

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

 Dim myArray myArray = Array(5, 3, 103, 99, -14, 12) Debug.Print Application.WorksheetFunction.Small(myArray, 1) 

如果你的数组是多维的,那么你将需要使用powershell迭代。

这是一个例子,你可以实现它:

 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