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


axlaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
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.
axlaAuthor Commented:
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
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
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

axlaAuthor Commented:
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
 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
You should be able to do it with the one-dimensional array

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.