代码之家  ›  专栏  ›  技术社区  ›  Ahmad

如何在自动筛选范围内查找最小值?

  •  1
  • Ahmad  · 技术社区  · 14 年前

    我有一列有两个值范围,特别是0-30000和60000+,我需要从中提取60000范围内最小的两个值。

    到目前为止我找到的唯一的使用方法 AutoFilter 生成要提取的所需数据的子集。我的问题是,自动筛选函数不返回对某个区域的引用。如果有,我可以用 SMALL 函数获取我要查找的值。

    如何从该筛选数据中筛选和提取两个最小值?

    3 回复  |  直到 14 年前
        1
  •  1
  •   Ahmad    14 年前

    我放弃了使用自动筛选的想法。相反,我结合了 SMALL 还有一个循环。

    Cells(2, secIdCol).Select
    Set valsRange = Range(Selection, Selection.End(xlDown))
    
    For Each val In valsRange
        If val.Value < 599999 Then
            val.Value = "" // I don't save changes 
            val1 = Application.WorksheetFunction.Small(valsRange, 1)
            val2 = Application.WorksheetFunction.Small(valsRange, 2)
        End If
    Next val
    
        2
  •  0
  •   Fionnuala    14 年前

    我认为你不一定需要VBA。是否考虑使用ctrl+shift输入的数组公式:

       =MIN(IF(A1:A7>2999,A1:A7))
    
        3
  •  0
  •   barrowc    14 年前

    这里有一个不使用 SMALL 工作表函数:

    With Worksheets("Sheet1")
        Dim lastRow As Long
        lastRow = .Cells(2, secIdCol).CurrentRegion.Rows.Count + 1
    
        Dim rowIndex As Long
        Dim currentValue As Long
        Dim val1 As Long
        Dim val2 As Long
    
        ' Set val1 to maximum possible long value
        val1 = 2147483647
        For rowIndex = 2 To lastRow
            currentValue = CLng(.Cells(rowIndex, secIdCol).Value)
            If (currentValue > 59999) Then
                If (currentValue < val1) Then
                    val2 = val1
                    val1 = currentValue
                End If
            End If
        Next rowIndex
    End With
    
    MsgBox val1 & " | " & val2