+ Reply to Thread
Results 1 to 6 of 6

Calculating a Weighted Median

  1. #1
    Registered User
    Join Date
    02-21-2012
    Location
    CA
    MS-Off Ver
    Excel 2010
    Posts
    1

    Calculating a Weighted Median

    Hi All,

    I'm new to VBA programming and need some help determining the weighted median (NOT weighted average...I know there is an existing function for this) for a data set.

    Here's my problem - I have two columns and Column A is filled with values (1, 4, 6, 12) while Column B shows the weight of those values (2, 3, 1, 4) respectively. If I want to calculate the median accurately, I need to account for the weight, meaning I can't use the median function for solely the values in Column A. What I need to do is calculate the median from the full unweighted list of these values which would look like the following (using the sample values from above): 1, 1, 4, 4, 4, 6, 12, 12, 12, 12.

    How can I write a VBA function that will take the values of Column A and copy them over to a new column using Column B as a reference for how many times it should be copied? Basically, how do I create the full unweighted list as shown above? From there, I could use a simple Median function to find the median of that column.

    I've included a sample data file. I want to use Column A and Column B to get Column C.

    Would appreciate any tips or sample code I could copy- much appreciated!

    -Katherine
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Calculating a Weighted Median

    Hello kaybee and welcome to the forum. Here is a user-defined function that will produce the results you're looking for, but will not populate another column (that's not necessary to do the calculation). Also I've attached your workbook with a working example. Thanks!
    Please Login or Register  to view this content.
    Attached Files Attached Files
    -Greg If this is helpful, pls click Star icon in lower left corner

  3. #3
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: Calculating a Weighted Median

    You could use a UDF to calculate the weighted median or you could do what you were in the process of doing and that is create a helper column. To use the UDF the formula is
    =WeightedMedian(A2:A13,B2:B13)
    The UDF is
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated?

  4. #4
    Registered User
    Join Date
    12-23-2011
    Location
    Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Thumbs up Re: Calculating a Weighted Median

    nevermind...
    Last edited by jimmypants; 04-20-2014 at 08:58 PM. Reason: Realized my mistake while testing/comparing both scripts. Both seem to work.

  5. #5
    Registered User
    Join Date
    08-07-2019
    Location
    ohio
    MS-Off Ver
    2016
    Posts
    1

    Re: Calculating a Weighted Median

    Hello,

    I tried using the above scripts on a table arrangement but does not appear to work. I'm basically trying to apply the weighted median on those rows that meet an index condition (2,4,6,....etc..)

    Any help would be appreciated to solve this issue.
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,570

    Re: Calculating a Weighted Median

    Administrative Note:

    Hello here4help2 and Welcome to Excel Forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

    Let us know if you have any questions.
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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