quarta-feira, 8 de fevereiro de 2012

Retorna a posição do maior valor dentro de um array

Ref: Notes/Domino 4 and 5 Forum


Function MaxIndex( inputArray As Variant ) As Integer
' ----------------------------------------------------------------------------------------------------------------------------------
' MaxIndex( ) written by Trent Overton - toverton@wgcinc.com
' ----------------------------------------------------------------------------------------------------------------------------------
' Description
' Finds the maximum value in an array and returns the index to that element.
' Note: Array must contain values which can be compared with standard operators.

' Parameters
' inputArray - array passed as variant data type
'
' Return value
' integer representing index to largest element in array.
' error conditions cause return value to be -1
'
' ---------------------------------------------------------------------------------------------------------------------------------- 

On Error Goto ErrorHandler

Dim rc As Integer
rc = -1
If Isarray( inputArray ) Then
Dim index As Integer
For index = Lbound( inputArray ) To Ubound( inputArray )
If rc = -1 Then 
rc = index
Elseif inputArray( index ) > inputArray( rc ) Then
rc = index
End If
Next
End If

MaxIndex = rc

ErrorHandler:
rc = -1
Resume Next

End Function

Nenhum comentário:

Postar um comentário