【问题标题】:How to get min value in a group?如何在组中获得最小值?
【发布时间】:2013-06-11 10:54:27
【问题描述】:

例子:

ID  Value   MAX
Group1  2   6
Group1  4   6
Group1  6   6
Group2  1   3
Group2  3   3
Group3  7   8
Group3  4   8
Group3  2   8
Group3  8   8
Group4  1   3
Group4  2   3
Group4  3   3
Group5  7   7

“MAX”列有我想要的结果。

我的两部分问题是:

(1) 如何获取“Max”列的值?

我目前正在使用数据透视表,但用户抱怨它太慢并且可能导致 Excel 无响应。

我尝试将数组函数与这样的公式一起使用:

=MAX(IF($A$9:$A$21=A12,$B$9:$B$21))

这不会保持最新状态,我需要一些机制来刷新数据。用户表示他们不希望再有一个按钮来刷新数据。

(2)假设有一个公式可以解决上述问题,我的Value列是一个可以为空的日期,我的要求也是获取组中的最小日期,忽略任何空白。

【问题讨论】:

    标签: excel excel-formula pivot-table array-formulas vba


    【解决方案1】:

    在C2中输入数组公式:

    =MAX(IF(A:A=A2,B:B))  
    

    然后抄下来。

    数组公式必须使用 Ctrl + Shift + Enter 而不仅仅是 Enter 键。如果正确执行此操作,公式将在公式栏中显示并带有花括号。

    【讨论】:

      【解决方案2】:

      一些事情...我的第一个问题是我现有的电子表格被设置为“手动计算”而不是“自动计算”。 (在菜单公式 | 计算选项下)。

      这里是一些示例代码,我用来添加基于另一列的“分组”计算最小日期。 (注意:我的电子表格有大约 1500 行,我确实注意到在更改单元格和更新公式时速度变慢了)

      Sub AddFormulaToCalculateEarliestRevisedDate()
          Application.Calculation = xlCalculationManual
          Application.ScreenUpdating = False
      
          Dim identifierColumn As String
          Dim identierRow As String
          Dim identifierRange As String
          Dim valueRange As String
          Dim formulaColumn As String
          Dim formulaRange As String
      
          Dim myIdentifierRange As Range
          Dim myFormulaRange As Range
          Dim lastRow As String
          lastRow = ActiveSheet.Range("C5000").End(xlUp).Row
      
          identifierColumn = "B"
          identifierRange = "B6:B" & lastRow
          valueRange = "AP6:AP" & lastRow
          formulaColumn = "CZ"
          formulaRange = "CZ6:CZ" & lastRow
      
          Set myIdentifierRange = ActiveSheet.Range(identifierRange)
          Set myFormulaRange = ActiveSheet.Range(formulaRange)
      
          ' delete any existing any array formulas first! otherwise, get error
          myFormulaRange.ClearContents
          myFormulaRange.NumberFormat = "m/d/yyyy;;" ' notice the ;; to handle zero dates 1/0/1900 to be blank
      
          ' loop through each row and set the array formula
          Dim identifierCell As String
          Dim arrayFormula As String
          Dim r As Range
          For Each r In myIdentifierRange.Rows
      
              ' example: arrayFormula = {=MIN(IF($B$6:$B$5000=B6,$AP$6:$AP$5000))}
              identifierCell = identifierColumn & r.Row
              arrayFormula = "MAX(IF(" & identifierRange & "=" & identifierCell & "," & valueRange & "))"
      
              Range(formulaColumn & r.Row).FormulaArray = "=" & arrayFormula
          Next
      
      
          Application.ScreenUpdating = True
          Application.Calculation = xlCalculationAutomatic
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2020-01-27
        • 1970-01-01
        • 2021-08-28
        • 1970-01-01
        • 2015-10-15
        • 2019-01-16
        • 2015-06-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多