Does anyone know of a UDF that performs a "Subtotal" median function?

jbaich

Board Regular
Joined
Nov 2, 2011
Messages
139
Hi all, there are many threads that reference this formula or something similar with the goal of emulating the Subtotal behaviour for Median. In my case, specifically I'm wanting the median function to only apply to visible cells when I'm using filters. The formula that I've been using and seems to work well is
Code:
{=MEDIAN(IF(SUBTOTAL(2,OFFSET(A2,ROW(A2:A500)-ROW(A2),0)),A2:A500))}

Obviously in this example the data range is A2:A500, I was wondering if anyone knows of a user defined function around this formula that will populate the range values based on the user selection like a regular function does? I was thinking something like;

Function MEDIANSUBTOTAL (datarange)

where if I entered "=MEDIANSUBTOTAL(S4:S30)" into my formula bar, the function would perform the operation as though I'd entered
Code:
{=MEDIAN(IF(SUBTOTAL(2,OFFSET(S4,ROW(S4:S30)-ROW(S4),0)),S4:S30))}

Has anyone already created or found a UDF for this function/purpose?

Thanks,
Joe
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Actually, I tested it and it seems to work. So maybe something like this...

Code:
Function MEDIANSUBTOTAL(r As Range)
    Dim v As Variant
    v = Evaluate("MEDIAN(IF(SUBTOTAL(2,OFFSET(" & r.Address & ",ROW(" & r.Address & ")-MIN(ROW(" & r.Address & ")),0[COLOR=#ff0000],1[/COLOR]))," & r.Address & "))")
    If IsError(v) Then
        MEDIANSUBTOTAL = CVErr(xlErrNum)
    Else
        MEDIANSUBTOTAL = v
    End If
End Function

Note that I've included the height argument (in red) for OFFSET.

Hope this helps!
 
Upvote 0
Thank You! Thank You! Thank You! :)

So far it seems to work perfectly!

You just made my month!

Cheers,
Joe
 
Upvote 0

Forum statistics

Threads
1,216,096
Messages
6,128,807
Members
449,468
Latest member
AGreen17

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