Excel MAXIF 函数或仿真?

发布于 2024-08-26 00:47:53 字数 269 浏览 3 评论 0原文

我在 中有一个中等大小的数据集,我从中希望提取 B 列中值的最大值,但这些值仅对应于 A 列中满足特定条件的单元格。

所需的功能与 SUMIFCOUNTIF 类似,但它们都不会返回必需的数据。没有 MAXIF 函数;我如何模仿一个?

I have a moderately sized dataset in 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 satisfy certain criteria.

The desired functionality is similar to that of SUMIF or COUNTIF, but neither of those return data that is necessary. There isn't a MAXIF function; how do I emulate one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡的优雅 2024-09-02 00:47:53

您可以使用数组公式。在要计算最大值的单元格中输入:=Max(If([test],[if true],[if false]),其中用测试替换方括号中的值,如果为 true,则返回什么;如果为 false,则返回什么。 例如:

=MAX(IF(MOD(A2:A25,2)=0,A2:A25,0)

在此公式中,如果值除以 2 没有余数,则返回 A 列中的值。请注意,我在比较和值中使用了一系列单元格。 。

现在,在编辑单元格时,按 Ctrl+Shift+Enter(同时按住 Ctrl 键和 Shift 键,然后按 Enter),

这将创建一个作用于单元格中的每个值的数组公式 ?

编辑 顺便说一句,您想以编程方式还是手动执行此操作?如果以编程方式,那么您使用什么环境?

编辑 如果通过 VBA,您需要什么 使用 FormulaArray 属性和 R1C1 引用,如下所示:

Range("A1").Select
Selection.FormulaArray = "=MAX(IF(MOD(R[1]C:R[24]C,2)=0,R[1]C:R[24]C,0))"

You can use an array formula.In the cell in which you want the max calculated enter: =Max(If([test],[if true],[if false]) where you replace the values in square brackets with the test, what to return if true and what to return if false. For example:

=MAX(IF(MOD(A2:A25,2)=0,A2:A25,0)

In this formula I return the value in column A if the value divided by 2 has no remainder. Notice that I use a range of cells in my comparison and in the value if false rather than a single cell.

Now, while still editing the cell, hit Ctrl+Shift+Enter (hold down the Ctrl key and the Shift together and then hit enter).

This creates an array formula that acts on each value in the range.

EDIT BTW, did you want to do this programmatically or manually? If programmatically, then what environment are you using? VBA? C#?

EDIT If via VBA, you need to use the FormulaArray property and R1C1 references like so:

Range("A1").Select
Selection.FormulaArray = "=MAX(IF(MOD(R[1]C:R[24]C,2)=0,R[1]C:R[24]C,0))"
半世蒼涼 2024-09-02 00:47:53

当您想要使用动态或命名范围时(例如,“当前行上方与当前行具有相同交易对手的行的最大应付金额),数组公式不能很好地工作。如果您不想使用对于数组公式,您始终可以求助于 VBA 来执行以下操作:

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

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
离鸿 2024-09-02 00:47:53

到目前为止提供的代码的一个限制是您仅限于 2 个条件。我决定进一步改进此代码,不限制 MaxIfs 函数的条件数量。请参阅此处的代码:

        Function MaxIfs(MaxRange As Range, ParamArray Criteria() As Variant) As Variant
        Dim n As Long
        Dim i As Long
        Dim c As Long
        Dim f As Boolean
        Dim w() As Long
        Dim k As Long
        Dim z As Variant

        'Error if less than 1 criteria
        On Error GoTo ErrHandler
        n = UBound(Criteria)
        If n < 1 Then
            'too few criteria
            GoTo ErrHandler
        End If
            'Define k
            k = 0            

        'Loop through cells of max range
        For i = 1 To MaxRange.Count

        'Start by assuming there is a match
        f = True

            'Loop through conditions
            For c = 0 To n - 1 Step 2

                'Does cell in criteria range match condition?
                If Criteria(c).Cells(i).Value <> Criteria(c + 1) Then
                    f = False
                End If

            Next c

            'Define z
            z = MaxRange

            'Were all criteria satisfied?
            If f Then
                k = k + 1
                ReDim Preserve w(k)
                w(k) = z(i, 1)
            End If

        Next i

        MaxIfs = Application.Max(w)

        Exit Function
        ErrHandler:
        MaxIfs = CVErr(xlErrValue)

    End Function

此代码允许 1 到多个条件。

该代码是参考 Hans V 在 Eileen's Lounge 发布的多个代码而开发的。

快乐编码

Diedrich

A limitation with the code provided thus far is that you are restricted to 2 conditions. I decided to take this code further to not restrict the number of conditions for the MaxIfs function. Please see the code here:

        Function MaxIfs(MaxRange As Range, ParamArray Criteria() As Variant) As Variant
        Dim n As Long
        Dim i As Long
        Dim c As Long
        Dim f As Boolean
        Dim w() As Long
        Dim k As Long
        Dim z As Variant

        'Error if less than 1 criteria
        On Error GoTo ErrHandler
        n = UBound(Criteria)
        If n < 1 Then
            'too few criteria
            GoTo ErrHandler
        End If
            'Define k
            k = 0            

        'Loop through cells of max range
        For i = 1 To MaxRange.Count

        'Start by assuming there is a match
        f = True

            'Loop through conditions
            For c = 0 To n - 1 Step 2

                'Does cell in criteria range match condition?
                If Criteria(c).Cells(i).Value <> Criteria(c + 1) Then
                    f = False
                End If

            Next c

            'Define z
            z = MaxRange

            'Were all criteria satisfied?
            If f Then
                k = k + 1
                ReDim Preserve w(k)
                w(k) = z(i, 1)
            End If

        Next i

        MaxIfs = Application.Max(w)

        Exit Function
        ErrHandler:
        MaxIfs = CVErr(xlErrValue)

    End Function

This code allows 1 to multiple conditions.

This code was developed with reference to multiple code posted by Hans V over at Eileen's Lounge.

Happy coding

Diedrich

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文