最直截了當的方法是使用一個循環來檢查每一個數組元素的值。
Function GetMaxIndicesArray(ByRef myArraySum() As Long, ByRef myArrayAddress() As Integer) As Integer
Dim i As Integer, j As Integer, iLow As Integer, iUp As Integer
Dim lMax As Long
iLow = LBound(myArraySum)
iUp = UBound(myArraySum)
lMax = Application.WorksheetFunction.Max(myArraySum)
j = 1
For i = iLow To iUp
If (myArraySum(i) = lMax) Then
myArrayAddress(j) = i
j = j + 1
End If
Next
GetMaxIndicesArray = j - 1
End Function
Sub test()
Dim myArraySum(1 To 4) As Long
Dim myArrayAddress(1 To 4) As Integer
Dim i As Integer, n As Integer
myArraySum(1) = 2
myArraySum(2) = 5
myArraySum(3) = 7
myArraySum(4) = 7
myArrayHigh = Application.WorksheetFunction.Max(myArraySum)
'myArrayAddress = "" 'code needed
n = GetMaxIndicesArray(myArraySum, myArrayAddress)
Debug.Print "myArrayHigh = " & myArrayHigh
For i = 1 To n
Debug.Print "myArrayAddress(" & i & ") = " & myArrayAddress(i)
Next
End Sub
然後在你調試程序窗口,只需鍵入
test
結果是這樣的:
myArrayHigh = 7
myArrayAddress(1) = 3
myArrayAddress(2) = 4
謝謝你的代碼後處理。因此,本質上沒有內建的方法或屬性可以使用工作表功能。最大或相似? – phreshsprout
不知道我的知識,甚至在google搜索了一些東西之後。 – jacouh