Median Calculation on multiple criteria

Commander Vimes

New Member
Joined
Aug 8, 2016
Messages
11
So i've been struggling with this. Is there a way to calculate the median of a set of values based on Name and volume? An example of the data layout is below, what i want is to calculate the median value for a large table with multiple entries using formula or VBA.

NameVolumeValue
Dog15
Dog12.5
Dog33
Cat12
Cat23.6
Duck55

<tbody>
</tbody>

Using the example above i would want to calculate the median for Dog, Cat and Duck.

Using Cat as an example, the figures there would be 1 x Cat=2 and 2 xCat=3.6.

Median would be 3.6: (2, 3.6, 3.6)

So is there a formula or macro Vba which would look at the Name and volume and then calculate the Mean of the value for each unique Name?

-CV
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Here's a function that should do it

Code:
Public Function cMEDIAN(ByVal cText As String, ByVal cRng As Range, Optional cVolume As Long = 1, Optional cValue As Long = 2)
    Application.Volatile True
    Dim cValues As Variant
    Dim i As Long
    i = 0
    ReDim cValues(i)
    For Each c In cRng
        If LCase(c.Value) = LCase(cText) Then
            For j = 1 To c.Offset(, cVolume).Value
                cValues(i) = c.Offset(, cValue).Value
                i = i + 1
                ReDim Preserve cValues(i)
            Next
        End If
    Next
    cMEDIAN = WorksheetFunction.Median(cValues)
End Function

Example usage if your data started in A1

=cMEDIAN("Cat",A2:A7)

There is optional parameters there to offset the Volume and Value column if you need them.
 
Upvote 0
Thanks Sericom, This looks great, just testing it out now.

As you mentioned, if the Vol and Val cols were both in Col C,D respectively rather than in B,C. Would i just need to add a +1 to the offset parameters. (if they were further along just increase as required?)

Are you able to elaborate on how this works? In particular how it picks up the Volume and then calcs the Median?
 
Upvote 0
If Name, Volume and Order are next to each other in that order, you won't need to add parameters. Doesn't matter if they aren't in Col A, just as long as they are grouped together.
The parameters are for offsetting the column from the name column.
eg. If name is in col A, Vol in col C and Val in Col D, then Vol is 2 columns away from A, and Val is 3 away from name so you'd use the following

=cMEDIAN("Cat",A2:A7,2,3)
 
Upvote 0

Forum statistics

Threads
1,216,140
Messages
6,129,105
Members
449,486
Latest member
malcolmlyle

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