+ Reply to Thread
Results 1 to 2 of 2

How to calculate medians using macro?

  1. #1
    Registered User
    Join Date
    06-20-2014
    Location
    France
    MS-Off Ver
    2010
    Posts
    20

    How to calculate medians using macro?

    Dear all,

    I am trying to seek help, in creating a macro which can automatically calculate medians for each name's values.

    I am manually calculating medians using formulas in column N, O & P for each name's values (column E). The name and values columns
    are generated through another macro, hence it has become a little tedious and time consuming in dragging and changing the cells for each median calculation, for larger datasets.

    Here are the formula's I am using to generate median calculations in column N, O & P.
    Please Login or Register  to view this content.
    I have also attached an example of datasheet and example of the output in the file below.
    median_test1.xlsm

    Any help would be highly appreciated.

    Many thanks.
    Last edited by jun22; 07-11-2014 at 07:07 AM.

  2. #2
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: How to calculate medians using macro?

    Quote Originally Posted by jun22 View Post
    I am trying to seek help, in creating a macro which can automatically calculate medians for each name's values.

    I am manually calculating medians using formulas in column N, O & P for each name's values (column E). The name and values columns are generated through another macro, hence it has become a little tedious and time consuming in dragging and changing the cells for each median calculation, for larger datasets.
    [....]
    I have also attached an example of datasheet and example of the output in the file below.
    First, the formulas in column O should be array-entered: press ctrl+shift+Enter instead of just Enter.

    They seemed to work (but return the wrong value) only because you normally-entered the formula into one of the rows referenced by the formula. If you copy the text of the formula in O3 from the Formula Bar (don't copy the cell) and paste it into O16, it will return a #VALUE error.

    Second, those formulas can be simplified as follows (again, array-enter): =MEDIAN(ABS(N3-E3:E6)), since you calculate =MEDIAN(E1:E6) in N3, and you intend to keep that since you reference N3 in column P.

    Finally, the VBA implementation might be easier if you integrate it into the "other macro" that generates the "name and values columns".

    The difficulty post facto is finding the range to be used in the MEDIAN formulas and the formulas in column P. I will use the names and values in columns A and E to delimit the ranges. But that is awkward and adds complexity.

    You say you want to "calculate medians" in a macro. I assume you want to create the MEDIAN and MAD formulas in columns N and O, as well as the formulas in column P. That way, the calculated values will adapt to any changes in the data as long as the number of data remains the same. It also makes it easier to understand ("audit") the calculations because we can see the formulas.

    Note: I took the liberty of optimizing your design, changing the formula in column O to 3.5*MAD so we do not have to repeat 3.5*O3 in each formula in column P.

    See the "Modified" worksheet in the attached file.
    Attachment 331595

    Right-click on the "Modified" worksheet tab, then click on "View Code".

    The code is duplicated below.
    Please Login or Register  to view this content.
    [EDIT] In hindsight, the statement firstRow=Cells(lastRow, "a").End(xlDown).Row can be simplified to firstRow=lastRow+1. (Klunk!)
    Attached Files Attached Files
    Last edited by joeu2004; 07-11-2014 at 03:43 PM. Reason: cosmetic; highsight improvement

+ 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. [SOLVED] First time user, need help with Medians!
    By steelavocado in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 03-07-2014, 03:41 PM
  2. [SOLVED] Medians for Incremental Ranges
    By Tams80 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-29-2014, 10:45 AM
  3. Medians with multiple ifs.
    By FoxyDread in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-24-2013, 01:30 PM
  4. Creating a macro to calculate medians for every 18-row range
    By niuren in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-13-2011, 07:41 PM
  5. Medians in Pivot Tables
    By Cortlyn in forum Excel General
    Replies: 0
    Last Post: 11-28-2007, 07:33 AM

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