Excel MAXIF function or emulation?

前端 未结 3 777
陌清茗
陌清茗 2021-01-13 05:19

I have a moderately sized dataset in excel from which I wish to extract the maximum value of the values in Column B, but those that correspond only to cells in Column A that

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-13 05:19

    Array formulas don't work very well when you want to use dynamic or named ranges (e.g., "the maximum amount due for rows above the current row that have the same counterparty as the current row). If you don't want to use an array formula, you can always resort to VBA to do something like this:

    Function maxIfs(maxRange As Range, criteriaRange As Range, criterion As Variant) As Variant
    
      maxIfs = Empty
      For i = 1 To maxRange.Cells.Count
        If criteriaRange.Cells(i).Value = criterion Then
            If maxIfs = Empty Then
                maxIfs = maxRange.Cells(i).Value
            Else
                maxIfs = Application.WorksheetFunction.Max(maxIfs, maxRange.Cells(i).Value)
            End If
        End If
      Next
    End Function
    

提交回复
热议问题