VBA Subtotal Median Function

Dan1445

New Member
Joined
Dec 21, 2011
Messages
40
Hello all,

I'm looking for a vba function that lets me have a subtotal that computes the median. I understand there is a method that uses "=MEDIAN(IF(SUBTOTAL(3,OFFSET(" but I am not looking for that. I want to be able to subtotal 10000+ lines of data with 700+ subtotals. When I record macro I get:

Columns("A:H").Select
Selection.Subtotal GroupBy:=2, Function:=xlCountNums, TotalList:=Array(7) _
, Replace:=True, PageBreaks:=False, SummaryBelowData:=True

But I'm looking for the function to be xlMedian. Thank you in advance for any help.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi Dan,

Sorry, but there isn't an xlMedian enumeration option- you'll need to find an alternative.

Perhaps you could use the SubTotal function with Count to create the Grouping Outline,
then use Replace to change the SubTotal Formulas to Median formulas.

Something like this....
Code:
Sub Median_SubTotals()
    With Sheets("Sheet1")
        With .Range("A1:H" & .Cells(.Rows.Count, "B").End(xlUp).Row)
            .Subtotal GroupBy:=2, Function:=xlCountNums, TotalList:=Array(7), _
                Replace:=True, PageBreaks:=False, SummaryBelowData:=True
        End With
        With .Range("G1:G" & .Cells(.Rows.Count, "G").End(xlUp).Row)
            .Replace What:="=SUBTOTAL(2,", Replacement:="=MEDIAN(", LookAt:=xlPart
            .Offset(0, -5).Replace What:="COUNT", Replacement:="MEDIAN"
            .Resize(1).Offset(.Rows.Count - 1).EntireRow.Delete
        End With
    End With
End Sub

This won't emulate all of SubTotal's behavior (like the option to exclude hidden cells or Grand Totals)
but hopefully it will be close enough for your purpose.
 
Upvote 0
Jerry,

I apologize for the late reply, as I was on vacation. This worked great. It does exactly what I want it to do. Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top