View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
R. Choate R. Choate is offline
external usenet poster
 
Posts: 106
Default VBA array - find largest element

I appreciate the attempts, but it did not work in my situation. Perhaps it had something to do with the dynamic resizing of the
arrays or something else that was in the more complicated actual code than what I gave in my example because I wanted to simplify
the specific problem and not sound confusing. In any case, I solved the issue so I won't bother you guys any further on my post. It
is academic at this point.

Thanks again,
--
RMC,CPA


"Dave Peterson" wrote in message ...
Norman's suggestion worked ok for me.

But Alan had a typo in his code: That last Arr should have been MyArray.

Option Explicit
Sub testme()

Dim Prog(1 To 3) As Long
Prog(1) = 15
Prog(2) = 0
Prog(3) = 2

Debug.Print "Max Value-- " & Application.Max(Prog)
Debug.Print "Index of Max Value-- " _
& Application.Match(Application.Max(Prog), Prog, 0) - 1 + LBound(Prog)

End Sub

Returned this in the immediate window:
Max Value-- 15
Index of Max Value-- 1



"R. Choate" wrote:

No, Norman's code does not work and the index number code indicated incorrect syntax in the editor. If I have an array where

Prog(1)
= 15, Prog(2) = 0, and Prog(3) = 2, Norman's code returns the "2" associated with Prog(3). I need for the code to return the "15"
after looping through all of them and determining that 15 was the largest element in the array. Here is the solution I finally
worked out on my own (Using option base 1):

High = 0
For A = 2 To BoxNums

If Prog(A) - 0 High Then
High = Prog(A)
Else
GoTo goaroundagain
End If

goaroundagain:

Next
If High = 0 Then
Z = Prog(1) + High
ElseIf High < Prog(1) Then
Z = Prog(1)
Else
Z = High
End If
--
RMC,CPA

"Alan Beban" wrote in message ...
R. Choate wrote:
This array is of variable size and contains variable contents. There is no easy situation like "Array(100, 150, 75, 95, 22)"

used
in
your example. My situation would be more like "Array(i)", where if i = 3, the array(i) would equal whatever is in that position
within the array. So I need the Max among all of the elements, represented by the variable i, as noted in my original post. For
instance, it would be more like array(1 to 50), where array(1) might be 62, array(15) might be 170, etc., etc.. Of course,

Array(1
to i) is not proper syntax.

If you want to return the *value* of the largest number in a 1-D array
named myArray, Norman Jones gave you the code:

Application.Max(myArray)

If you want to return the *array index number* of the largest number in
the array, then, you can use

Application.Match(Application.Max(myArray), myArray, 0) - 1 + LBound(arr)

Alan Beban


--

Dave Peterson