2015-01-21 41 views
1

這裏的基本思想是roll 4 d6,降到最低。如何在VBA中找到最小的數組,而不是在excel範圍

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 

我發現如何找到在Excel中的最低值很多信息,通過定義範圍後加入.Small,但我不認爲在這裏工作。這可能是因爲我如何完成這個任務,但我太新鮮了,我真的不知道。

感謝

回答

3

假設你的數組是一維數組,你仍然可以使用worksheetfunction類,如:

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

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

如果陣列是多維的,那麼你就需要使用蠻力迭代。

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 
相關問題