我有一个名为Num_Key的 VBA集合(Num_Key作为新集合),它将在其中存储唯一的数字。
有没有办法在没有循环的情况下从 Num_Key VBA集合中获取最小和最大数字?
提前谢谢你:)
答案 0 :(得分:5)
WorksheetFunction.Max
或WorksheetFunction.Min
。像这样:
Option Explicit
Sub TestMe()
Dim myCollection As New Collection
myCollection.Add 1
myCollection.Add 2
myCollection.Add 100
myCollection.Add 50
Debug.Print WorksheetFunction.Max(CollectionToArray(myCollection))
End Sub
Public Function CollectionToArray(myCol As Collection) As Variant
Dim result As Variant
Dim cnt As Long
ReDim result(myCol.Count - 1)
For cnt = 0 To myCol.Count - 1
result(cnt) = myCol(cnt + 1)
Next cnt
CollectionToArray = result
End Function