+ Reply to Thread
Results 1 to 15 of 15

Need a UDF to calculate 'weighted median'

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Need a UDF to calculate 'weighted median'

    Hello everyone,

    I am very new to vba and need a UDF to calculate 'weighted median'. You know 'weighted average', but 'weighted median' is different which is 'the 50% weighted percentile'.

    Suppose there are two columns: column A for weight (%), and column B for age. To manually get the 'weighted median', I need to do two steps:

    1) sort data from the highest to the lowest value based on column B.

    2) from the first cell in column A, keep adding up the weights until it reached a total of 50%. The cell in column B which makes up the total weight of 50% is the value 'weighted median'.

    Can a UDF to accomplish the above two steps to get the value I want? The UDF will allow me to get the 'weighted median' by specifying: =WTMEDIAN(A1:A20,B1:B20).

    An example is attached here.

    image001.png
    Attached Files Attached Files

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: Need a UDF to calculate 'weighted median'

    What means "Sum up weights until the total weights become around 50%."

    Is that closest to 50% (like 51% rather than 48%).

  3. #3
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: Need a UDF to calculate 'weighted median'

    Hi zbor,

    Thanks for your help. As 51% is closer to 50% than 48%, then 51% is preferred.

  4. #4
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: Need a UDF to calculate 'weighted median'

    Here, try this:
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: Need a UDF to calculate 'weighted median'

    Thanks, zbor. The separate steps work.

    Is it possible that we can have a nice vba UDF to achieve the task? For example, it will give me the results if I type: =WTMedian(A1:A100,B1:B100).

    It will be very interesting to see this important static implemented by a vba UDF.

  6. #6
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Need a UDF to calculate 'weighted median'

    Hi Alice, got a UDF here for you. Needed to introduce a sorting algorithm in it - but it seems to be working

    Please Login or Register  to view this content.
    Also see attached. Just hit F9 and see it working. I also did a check by manual calculations to see that it's working. See cell P12 in particular.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: Need a UDF to calculate 'weighted median'

    Thank you, quekbc. It seemed to be working. You are right, the sorting algorithm seems complicated to understand.

    I appreciate if if anyone can really make a very nice UDF.

  8. #8
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Need a UDF to calculate 'weighted median'

    Try this:-
    Slight re-think, Function now based on columns "A & B" (A2:B18)
    Please Login or Register  to view this content.
    Last edited by MickG; 07-28-2015 at 10:46 AM.

  9. #9
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: Need a UDF to calculate 'weighted median'

    Hi, MickG, I really feel the macro looks great: to achieve the task with minimal lines of code. Thank you.

    Before I make it as solved, can you also consider the case of text values and errors? Obviously we only deal with numeric values, but in some situations there may appear errors or text values.

  10. #10
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Need a UDF to calculate 'weighted median'

    Can you show an example ??

  11. #11
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: Need a UDF to calculate 'weighted median'

    Hi MickG, please see the new example attached here.

    For any row with either error or text value, just exclude them.

    The two columns for weight (%) and age are not next to each other in my case. But it does not matter if you want to put them together.
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: Need a UDF to calculate 'weighted median'

    Hi MickG, for any cases with error or text values, just exclude them. How can I add this to the original code?

    Many thanks for your help. I'm still learning

  13. #13
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Need a UDF to calculate 'weighted median'

    Try this:-
    NB:- You need to put all 3 columns in the function, in this case "A2 to C12"
    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    01-16-2014
    Location
    London
    MS-Off Ver
    Excel 2003 Excel 2010
    Posts
    189

    Re: Need a UDF to calculate 'weighted median'

    Hi MickG, I'm so pleased that the macro works nicely. It is really useful to me.

    The macro will be useful for other starters to learn as well

  15. #15
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Need a UDF to calculate 'weighted median'

    You're welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calculating a Weighted Median
    By kaybee in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-14-2019, 06:35 AM
  2. How to do a weighted median?
    By bnelson333 in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 02-01-2016, 01:59 PM
  3. [SOLVED] Using Median IF to calculate the median for a specific criteria
    By boynejs in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 10-20-2014, 01:50 AM
  4. Weighted Median with a condition (group by)
    By jaganmskcc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-27-2012, 06:07 PM
  5. Won't calculate Median Value
    By braydon16 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 12-09-2010, 06:00 PM
  6. calculate median with IF AND
    By jy2009 in forum Excel General
    Replies: 3
    Last Post: 05-30-2010, 08:40 PM
  7. Calculate median
    By JoshuaSQ in forum Excel General
    Replies: 4
    Last Post: 11-15-2007, 08:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1