VBA - 每个地区的前10%

时间:2015-07-11 04:26:04

标签: vba excel-vba if-statement nested-if excel

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

我正在编写一个宏,它将在收入IF旁边的空单元格中输入日期,该收入位于该区域的前10%,而不是最近审核的。这就是我遇到困难的地方。

我可以编写一行代码来返回收入的前10%,但无法弄清楚如何返回一个区的前10%。

作为一个功能,我可以达到如下所需的结果: {=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语句定义范围,或者获得与我上面粘贴的函数等效的工作表函数。

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

1 个答案:

答案 0 :(得分:0)

我能想到的最好的工作是使用如下函数: {=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