+ Reply to Thread
Results 1 to 11 of 11

Find the median, mode

  1. #1
    Registered User
    Join Date
    06-29-2017
    Location
    CASA
    MS-Off Ver
    2007
    Posts
    13

    Exclamation Find the median, mode

    Hello

    please i want to know how i can create function with vba editor to find the median and mode for the following data.

    5, 5, 10, 10, 10, 10, 15, 15, 20 ,500
    Last edited by exc05; 07-04-2017 at 08:12 AM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Find the median, mode

    Excel already has Median and Mode functions builtin.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Find the median, mode

    If this is an extension of the previous thread, https://www.excelforum.com/excel-pro...-function.html then part of the question is how to do it without resorting to the built in functions.

    Seeing that the list is sorted (will it always be sorted?) and small, will a simple, brute force type algorithm work? This page ( http://www.codinghelmet.com/?path=exercises/array-mode ) suggests a simple algorithm for sorted data (see the section titled "Quicksort solution"). The basic idea looks something like:
    1) Store the first data point with a count of 1
    2) Loop through the elements of the array and test if the current element is the same as the previous element.
    2a) If the current element is the same as the previous, increment count for current value by 1
    2b) If the current element is different from the previous, then check if the count for the current value is greater than the currently stored max count. If so, then store current value and count as mode. If not, then resume loop.
    3) At the end of the loop you should have the mode of the array.
    4) Median is simply the middle value if n is odd. It is the average of the two middle values if n is even.

    Do you have specific questions about coding that in VBA? Do you need the algorithm to be able to handle multi-modal data (where there can be more than one possible mode)? Will the data always be sorted, or do you need a sorting algorithm or an algorithm that does not require the data array to be sorted? As I indicated in the other thread, there are many considerations in developing median and mode algorithms. It will be easier to help if you can narrow down your question to specific parts of your chosen algorithm rather than this kind of broad question.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    06-29-2017
    Location
    CASA
    MS-Off Ver
    2007
    Posts
    13

    Re: Find the median, mode

    i can calculate mode and median simply in excel VBBA.png , but what i want is to customize a function for median and mode with vba editor to calculate mode and made like excel Vba.png

  5. #5
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Find the median, mode

    Why do you want to do that?

    is it just for fun?
    Or some school project?
    Or is it because you want to slightly edit how median and mode work?
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  6. #6
    Registered User
    Join Date
    06-29-2017
    Location
    CASA
    MS-Off Ver
    2007
    Posts
    13

    Re: Find the median, mode

    Project school , i try to doing it but i can't

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Find the median, mode

    This works for the median:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  8. #8
    Registered User
    Join Date
    06-29-2017
    Location
    CASA
    MS-Off Ver
    2007
    Posts
    13

    Re: Find the median, mode

    Thank you very much bro , any idea about mode

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Find the median, mode

    This works for me:

    Please Login or Register  to view this content.
    Last edited by xladept; 07-03-2017 at 09:03 PM.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Find the median, mode

    Another one.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  11. #11
    Registered User
    Join Date
    06-29-2017
    Location
    CASA
    MS-Off Ver
    2007
    Posts
    13

    Re: Find the median, mode

    Thank you all for helping me solve this

+ 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. Mode and Median function
    By exc05 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-29-2017, 11:04 PM
  2. [SOLVED] Median and Mode with Criteria
    By rpjwhite in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-15-2016, 11:23 AM
  3. [SOLVED] Mode If and Median If help
    By bobthebofifn in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-19-2014, 01:44 PM
  4. Median Indirect: Find median in range and bring back adjacent cells
    By Keelin in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 06-27-2014, 08:31 AM
  5. [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
  6. Median and mode
    By maestrodos in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-01-2012, 10:40 PM
  7. Mean, median and mode
    By brandon in forum Excel General
    Replies: 6
    Last Post: 05-06-2008, 12:39 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