Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return the second highest value in array across a series of 10 fields

Status
Not open for further replies.

liamthequietman

Technical User
Feb 26, 2018
1
US
thread707-1496731
Brilliant code MakeItSo!

The code works great except if the first field is a zero or all the fields in the row contain zeros (see pic).

Many thanks!

Function Maximum(ParamArray FieldArray() As Variant)
' Declare the two local variables.
Dim I As Integer
Dim currentVal As Variant
Dim secondHighest As Variant

' Set the variable currentVal equal to the array of values.
currentVal = FieldArray(0)

' Cycle through each value from the row to find the largest.

Dim tmpArray
For I = 0 To UBound(FieldArray)
If FieldArray(I) > currentVal Then
currentVal = FieldArray(I)
End If
Next

tmpArray = Filter(FieldArray, currentVal, False, vbTextCompare)
'This will fill the tmpArray with all your array values EXCEPT the highest one.

secondHighest = tmpArray(0)
For I = 0 To UBound(tmpArray)
If tmpArray(I) > secondHighest Then
secondHighest = tmpArray(I)
End If
Next

' Return the maximum value found.
Maximum = secondHighest

' Expr1: Maximum ([nPP1CSF],[nPP2CSF],[nPP3CSF],[nPP4CSF],[nPP5CSF],[nPP6CSF],[nPP7CSF],[nPP8CSF],[nPP9CSF],[nPP0CSF])

End Function
 
 http://files.engineering.com/getfile.aspx?folder=2da1788d-4ee0-45c6-8403-e98cda649909&file=zMAX-SECOND.PNG
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top