首页 > 解决方案 > 如何在特定范围内获得小值

问题描述

在此处输入图像描述

我想在D 列上的日期中获得第 1、第 2、第 3、...第 n... 个小值。但是有一个特定的类别显示在E 列中。我写了一个代码来获得一个特定的范围调用 p。它正在工作。这意味着当类别 C 时,它仅选择范围内的 C 值。当它等于 F 时,它只得到 F 值。

现在我想获得第一个小值的行号。但是C类它给出了正确的小值。但它变成了 F 类,它再次给出了以前的结果。谁能帮我解决这个问题。?

dim p as range, c as range, i as integer, irow as long
dim ary(1 to 5) as varient 

ary(1) = "C"
ary(2) = "F"
ary(3) = "B"
ary(4) = "PC"
ary(5) = "BC"

For i = 1 To UBound(ary)
cat = ary(i)
Set p = Nothing
Set c = Nothing
    For Each c In Range("E: E")
        If c.Value = cat Then
            If p Is Nothing Then
                Set p = c.Offset()
            Else
            Set p = Union(p, c)
            End If
        End If
    Next c

irow = Application.WorksheetFunction.Match(WorksheetFunction.Small(p.Offset(, -1), 1), Range("D:D"), 0)
Cells(4, 12) = Cells(irow, 5)
next i

标签: excelvbarangeworksheet-function

解决方案


推荐阅读