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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

max value in array - built in function?

Status
Not open for further replies.

ctlin

Technical User
Apr 17, 2002
77
US
is there a built in function in VB that returns the max value and its position in an array?
i seem to remember this being in other languages but haven't found it for VB yet.
if all else fails, i suppose the few lines of code needed could be written myself - i just figure that somebody else has probably done it better before me.
 
VB doesn't even have a builtin function to look for the maximum one of two variables, let alone to search for the maximum value within an array

following trivial code should do the job:
Public Sub main()
Const seedvalue As Long = 12345678
Dim a(9) As Long
Dim i As Integer
For i = 0 To 9
a(i) = Round(seedvalue * Rnd, 0)
Debug.Print a(i)
Next i

Dim maxvalue As Long
Dim uba As long
maxvalue = a(0)
uba = ubound(a)
For i = 1 To uba
maxvalue = max(maxvalue, a(i))
Next i

Debug.Print vbCrLf, maxvalue

End Sub

Private Function max(a1 As Long, a2 As Long) As Long
If a1 >= a2 Then
max = a1
Else
max = a2
End If
End Function


_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Faster than actually calling a function, would be to use an immediate IF statement.

maxvalue = IIf((maxvalue > a(i)), maxvalue, a(i))
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top