This adds a "scratch" sheet and does all the work on this sheet. Then it
writes the results where you specified.
I have commented out the code to delete the added sheet. You can look at it
to make sure the macro is getting the correct values. Once you are
satisfied the macro works, you can uncomment the sh1.Delete code and the
macro will delete the scratch sheet. On the scratch sheet, the integers are
listed in column C and the frequencies in D
In C2 is the 4th highest occuring value down to the highest value
In D2 is the lowest value down to the 4th lowest.
Sub GetExtremes()
Dim sh As Worksheet, sh1 As Worksheet
Dim rng1 As Range, Large4 As Long, Small4 As Long
Dim rngSmall As Range, rngLarge As Range
Dim resSmall As Variant, resLarge As Variant
Set sh = ActiveSheet
Set sh1 = Worksheets.Add
sh1.Range("A2:A200").Value = sh.Range("A2:A200").Value
sh1.Range("A201:A399").Value = sh.Range("B2:B200").Value
sh1.Range("A2:A400").Sort Key1:=sh1.Range("A2"), _
Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
sh1.Range("A1").Value = "Header1"
sh1.Range("C1").Value = "Header1"
sh1.Range("A1:A399").AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Range("C1"), _
Unique:=True
Set rng1 = sh1.Range(sh1.Range("C2"), _
sh1.Range("C2").End(xlDown))
rng1.Offset(0, 1).Formula = "=Countif(A:A,C2)"
rng1.Offset(0, 1).Formula = rng1.Offset(0, 1).Value
rng1.Resize(, 2).Sort Key1:=rng1.Offset(0, 1), _
Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Large4 = Application.Large(rng1.Offset(0, 1), 4)
Small4 = Application.Small(rng1.Offset(0, 1), 4)
resLarge = Application.Match(Large4, rng1.Offset(0, 1), 0)
Set rngLarge = sh1.Range(rng1(resLarge), rng1(rng1.Count))
sh.Range("C2").Resize(rngLarge.Count, 1).Value = _
rngLarge.Value
resSmall = Application.Match(Small4, rng1.Offset(0, 1), 1)
Set rngSmall = sh1.Range(rng1(1), rng1(resSmall))
sh.Range("D2").Resize(rngSmall.Count, 1).Value = _
rngSmall.Value
sh.Activate
Application.DisplayAlerts = False
'sh1.Delete
Application.DisplayAlerts = True
End Sub