Solved

# Questions on Max Function in VBA

Posted on 2008-11-13
530 Views
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

0
Question by:axla

LVL 17

Expert Comment

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.
0

Author Comment

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
0

LVL 17

Expert Comment

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

0

Author Comment

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

0

LVL 17

Accepted Solution

You should be able to do it with the one-dimensional array
0

## Featured Post

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…