VBA - 每个地区前 10%

VBA - Top 10 percent of each region

我有一个 Excel 2013 年的电子表格,其中显示了不同地区资产的收入。

我正在编写一个宏,如果该收入在该地区的前 10% 中并且最近没有被审查,它将在收入旁边的空单元格中输入一个日期。这就是我发现困难的地方。

我可以写一行代码return前十的收入,但想不出如何return一个区的前十。

作为一个函数,我可以实现如下想要的结果: {=LARGE(IF(I13:I1000=800,IF(AO13:AO1000<>DATE(2015,3,12),AI13:AI1000,""),""), 17)}

对于我的宏,我编写的代码将:

1) 接受将审查哪个学区和审查日期的输入

2) 确定上次审核的时间(有点,同样的问题,我需要找到某个地区资产的最大值)

3) 计算需要审核的资产数量(前 10% 和后 20%)

这是我目前的代码:

Sub ScheduleNextReview()
'Determine which district will be reviewed
    Dim Dist As String
    Dim NextDate As Date

    Dist = InputBox(Prompt:="Enter district for review:", Title:="Dist:", Default:="000")
    NextDate = InputBox(Prompt:="Date of Review", Title:="Enter Date for next review:", Default:="mm/dd/yyyy")

'Find date of Last Review
    Dim rng As Range
    Dim LastReview As Date
    Set rng = ActiveSheet.Range("AL13:AL1000")
    LastReview = Application.WorksheetFunction.Max(rng) 'need to figure out how to get the max value for those in Dist

'Count number of wells in district and find top ten percent and bottom twenty percent
    Dim DistTtl As Double
    Dim TopTenth As Integer
    Dim BottomTwent As Integer

    DistTtl = WorksheetFunction.CountIf(Range("I13:I10000"), Dist)
    TopTenth = WorksheetFunction.Round(DistTtl / 10, 0)
    BottomTwent = WorksheetFunction.Round(DistTtl / 5, 0)

MsgBox "There are " & TopTenth & " assets in the top ten percent, and " & BottomTwent & " assets in the bottom twenty percent of " & Dist & " for review on " & NextDate & "."
End Sub

我正在努力弄清楚如何使用 IF 语句定义范围或获取与我在上面粘贴的函数等效的工作表函数。

如果有任何需要进一步说明的地方,请告诉我 谢谢!

我能想到的最好的解决方法是使用如下函数: {=PERCENTILE.INC(IF(I13:I999=100,AI13:AI999,""), 0.9)}

我为每个选区执行了这个函数,然后创建了一系列 ElseIf 语句以获得所选选区的第 90 个百分位数:

            Dim Dist90 As Double
            Dim Dist As Integer

        If Dist = 100 Then
            Dist90 = Cells(2, 48)
        ElseIf Dist = 200 Then Dist90 = Cells(3, 48)
        ElseIf Dist = 300 Then Dist90 = Cells(4, 48)
        ElseIf Dist = 400 Then Dist90 = Cells(5, 48)
        ElseIf Dist = 500 Then Dist90 = Cells(6, 48)
        ElseIf Dist = 600 Then Dist90 = Cells(7, 48)
        ElseIf Dist = 700 Then Dist90 = Cells(8, 48)
        ElseIf Dist = 800 Then Dist90 = Cells(9, 48)
        End If