Link to home
Start Free TrialLog in
Avatar of axla
axla

asked on

Questions on Max Function in VBA

I have a block of data points with monthly values for several categories across the year.  The code snippet listed below does what I want, but I believe there's a more efficient way of doing this using a "Max" function.  However, I was unable to get the Max function working when I attempted to use it in my code.  Any help would be greatly appreciated.


Public Sub findMaxVal()

Dim var(1 To 12)
Dim varMax As Double
Dim cnt As Long
Dim hold As Long
Range("e2").Select

Do Until cnt = 12
cnt = cnt + 1
var(cnt) = ActiveCell
    If cnt = 1 Then
            varMax = var(cnt)
    ElseIf cnt <> 1 Then
    If varMax < var(cnt) Then
         varMax = var(cnt)
    End If
    End If
         ActiveCell.Offset(0, 1).Select
Loop
        MsgBox varMax
End Sub


Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

You will need to use the application worksheet object to reference any functions like that.

similar to this one
Application.WorksheetFunction.Count(oRange)
or
 myvariable =  Application.WorksheetFunction.Max(oRange)

Define the range, and then address it something like above. Hopefully that will get you moving in the right direction.
Avatar of axla
axla

ASKER

xDJR1875,

Thanks.  I modified my code and I can identify the maximum value.  However, I forgot to mention that I also need to identify which index value in the array corresponds to the maximum value.  Can you help me with the syntax?

Aaron


Public Sub findMaxVal()

Dim var(1 To 12)
Dim varMax As Double
Dim cnt As Long
Dim hold As Long
Dim holdr As String
Range("e2").Select

Do Until cnt = 12

    cnt = cnt + 1
    var(cnt) = ActiveCell
    ActiveCell.Offset(0, 1).Select

Loop

    hold = Application.WorksheetFunction.Max(var())
    holdr = var().Index.Value
    MsgBox hold & " - " & (cnt)

End Sub
Here is some info on searching an array in vba...
Searching for Array Elements
Visual Basic provides a mechanism finding elements in an array. The Array.BinarySearch() function searches the specified array for a matching element and returns the index of the matching element:

Dim strColors() = {"Red", "Green", "Blue"}
Dim intMatchIndex As Integer

intMatchIndex = Array.BinarySearch(strColors)

But a simple foreach loop will also do... similar to the following...

A For loop can also be used to iterate through each element of an array:

Dim intCount As Integer

For intCount = 0 To 12
     If var(intCount) = hold Then MsgBox hold & "-" & var(intCount) & "-" & str(intCount)
Next intCount

Avatar of axla

ASKER

xDJR1875,

Since I'm already looping through the Array, do I need to reformat the code block even though I can already identify the maximum value?  Conceptually, I realize that I just need to return the index number of the Array value that corresponds to the maximum value.  Can I extract the index value from a one-dimensional array, or do I need to create a two-dimensional array?

Aaron
 
ASKER CERTIFIED SOLUTION
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial