I have an Excel 2013 spreadsheet that, among other things, shows the Income for assets in different Districts.
I'm writing a macro that will enter a date in an empty cell next to the income IF that income is in the top 10 percent for that District and not recently reviewed. And that's where I'm finding difficulty.
I can write a line of code to return the top ten percent of income, but can't figure out how to return the top ten percent of one District.
As a function, I can achieve the desired result as follows:
{=LARGE(IF(I13:I1000=800,IF(AO13:AO1000<>DATE(2015,3,12),AI13:AI1000,""),""), 17)}
For my macro, I have written code that will:
1) Accept input for which district will be reviewed and the date of review
2) Determine when the last review was conducted (kind of, same problem, I need to find the max for assets that are in a certain district)
3) Calculate how many assets need to be reviewed (top ten percent and bottom twenty percent)
This is the code that I have so far:
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
I'm struggling to figure out how to define a range with an IF statement or get the worksheetfunction equivalent of the function I pasted above.
Please let me know if anything requires further clarification Thanks!