+ Reply to Thread
Results 1 to 14 of 14

VBA for Median calculation based off specific year

  1. #1
    Registered User
    Join Date
    02-01-2021
    Location
    Birmingham
    MS-Off Ver
    2016
    Posts
    27

    Lightbulb VBA for Median calculation based off specific year

    Hello everyone,
    I am looking for a way to code for a median calculation in a VBA user defined function.
    The end goal is to have annual averages calculated from a data set, so values included for each calculation are based on the year a value was recorded (in cell offset(0,1)). We have been able to do this for the mean calculations by using the code:

    Please Login or Register  to view this content.
    We also need to calculate each annual median... I am thinking along the lines of listing the values, sorting the values and outputting the middle one but have no idea how to go about coding for this?? Any ideas would be appreciated

    Side note: how do I input the above code in this post as code rather than text? Sorry am new to using this forum
    Thank you
    Last edited by lilybickel; 03-03-2021 at 07:52 AM. Reason: solved

  2. #2
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: VBA for Median calculation based off specific year

    Pl put the code in Code tags.
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  3. #3
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: VBA for Median calculation based off specific year

    I feel it is not median it is the average of the year as your code goes.
    Change these lines
    Please Login or Register  to view this content.
    as
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    02-01-2021
    Location
    Birmingham
    MS-Off Ver
    2016
    Posts
    27

    Re: VBA for Median calculation based off specific year

    Hi,
    Sorry that was a typo the code should say +1 there. You are right this is my code for finding the mean, however I need to adapt it to also find the median and it is there that I am stuck.
    Thanks

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: VBA for Median calculation based off specific year

    Pl upload a sample file.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: VBA for Median calculation based off specific year

    UDF

    Use in cell like
    =GetAVR_MEDI(A2:B1000,2020)
    where A2:A1000 has value to be calculated, B2:B1000 has date.
    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: VBA for Median calculation based off specific year

    Code for Median. Result cell should be formatted for date.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by kvsrinivasamurthy; 02-24-2021 at 11:23 AM.

  8. #8
    Registered User
    Join Date
    02-01-2021
    Location
    Birmingham
    MS-Off Ver
    2016
    Posts
    27

    Re: VBA for Median calculation based off specific year

    Hello, thank you both very much for your help - sorry it has taken me so long to get back to you.
    I have put both codes into an example file and kvsrinivasamurthy your code works best for what I'm after, however I need the output to be the median test score rather than the date. I have attached the file for reference now.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: VBA for Median calculation based off specific year

    here is new code
    Please Login or Register  to view this content.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    02-01-2021
    Location
    Birmingham
    MS-Off Ver
    2016
    Posts
    27

    Re: VBA for Median calculation based off specific year

    Perfect, thank you so much
    I have a few tests where there are multiple columns with calculations such as ratings of the test score and we are also looking to find the median of some of these column values.
    The example sheet I have included shows Test score|Score rating|Date so we I would be looking to find the median score as well as the median rating based on the year of the date. Some of the tests I am working with have up to 17 columns per single instance where I am looking to calculate the median of the values in the 14th column prior to each date for example.
    Would you mind explaining what the VBA code is doing so I can adapt accordingly for each test? I have tried playing around with it and can't figure it out.
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: VBA for Median calculation based off specific year

    Pl see file with codes for UDF's AnnScoreMdn and AnnRatingMdn
    Attached Files Attached Files
    Last edited by kvsrinivasamurthy; 03-02-2021 at 01:20 AM.

  12. #12
    Registered User
    Join Date
    02-01-2021
    Location
    Birmingham
    MS-Off Ver
    2016
    Posts
    27

    Re: VBA for Median calculation based off specific year

    The code is working for the AnnScoreMdn but not for AnnRatingMdn; please see attached file (AnnScoreMdn was being used for both sections in your previous file) I can't tell what the problem is?
    Attached Files Attached Files

  13. #13
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: VBA for Median calculation based off specific year

    In the columns for rating formulas are there. in the formulas all numbers are written as text. Eg for 1 it is written as "1". Similarly all numbers. I have changed them as numbers.
    In the columns for median for rating score median formula was used. I have corrected it.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    02-01-2021
    Location
    Birmingham
    MS-Off Ver
    2016
    Posts
    27

    Re: VBA for Median calculation based off specific year

    Ah I hadn't realised that would make a difference - thank you for all your help, it is all working perfectly now

+ 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. Replies: 8
    Last Post: 08-09-2016, 12:50 AM
  2. [SOLVED] Calculation based on the day of the year, not the date, for projecting results
    By DianeP in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-22-2015, 08:56 AM
  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. [SOLVED] Median Ifs, need to find median $ amount per deal for each year
    By xenomorph8472 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-13-2012, 02:01 PM
  5. [SOLVED] Is the IRR calculation based on cash flows at beginning of year?
    By sammad in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 06:05 PM
  6. [SOLVED] Is the IRR calculation based on cash flows at beginning of year?
    By sammad in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 PM
  7. [SOLVED] Is the IRR calculation based on cash flows at beginning of year?
    By sammad in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 01:05 PM

Tags for this Thread

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