Full Version: How To Find The Maximum Value In A Column With Multiple Conditions?
UtterAccess Discussion Forums > Microsoft® Office > Microsoft Excel
Javiator
Hello,

I need to find the maximum value in a column of a worksheet, where certain values are present in other columns. For example, I would need to find the maximum value in column F among rows where column C = "John Doe", column D = "Assessment", and column E = "1/1/2012". Could someone please suggest the proper strategy to accomplish this?

Thanks so much for your assistance!
MedicalServices
I was browsing around and saw this code by John Walkenbach. I thought it may be of some use to you regarding your question. It uses the .Find function to go to the maximum value...

CODE
Sub GoToMax()
'   Activates the cell with the largest value
    Dim WorkRange as Range
'   Exit if a range is not selected
    If TypeName(Selection) <> "Range" Then Exit Sub
'   If one cell is selected, search entire worksheet;
'   Otherwise, search the selected range
    If Selection.Count = 1 Then
        Set Workrange = Cells
    Else
        Set Workrange = Selection
    End If
'   Determine the maximum value
    MaxVal = Application.Max(Workrange)    
'   Find it and select it
    On Error Resume Next
    Workrange.Find(What:=MaxVal, _
        After:=Workrange.Range("A1"), _
        LookIn:=xlValues, _
        LookAt:=xlPart, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False _
        ).Select
    If Err <> 0 Then MsgBox "Max value was not found: " & MaxVal
End Sub


If you sorted your sheet by "C", "D", then "E" that should group them to where you can make a selection to pass to the 'WorkRange' variable.
dflak
It gets ugly.

Here is the single criteria version of MAXIF(). Enter it as an array formula (CTRL-SHIFT-ENTER).

=MAX(IF($F$1:$F$15=A12,IF(NOT(ISBLANK($G$1:$G$15)),$G$1:$G$15)))

In this case, the thing you want the maxium of is in Range G1:G15. What you want to evaluate is in Range F1:F15. What you want to match to is in Cell A12.

To extend the formula to more criteria, you would have to nest the first if statement.

=MAX(IF($F$1:$F$15=A12,IF(SecondRange = Second Criteria, IF (Third Range = Third Criteria , ...IF(NOT(ISBLANK($G$1:$G$15)),$G$1:$G$15)))

I leave it as an exercise to the student to figuer out where to close all those extra parenthesis. I think they'd come at the end. Any formula that closes with six parenthesis can't be pretty.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.