+ Reply to Thread
Results 1 to 13 of 13

How to find Second Mode across various columns

  1. #1
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2007
    Posts
    113

    How to find Second Mode across various columns

    Hi,

    I am having a difficult time trying to find a way to write a formula that would essentially give me the 2nd mode across a range of columns that actually skips columns in between.

    For example: my current mode formula looks like this:
    =MODE(F2,I2,L2,O2,R2)

    As you can tell, this is not a consecutive range, and the mode is basically derived from every 3rd column in between F2 and R2.

    What I can't figure out is how to create a 2nd mode, or a 3rd mode for that matter.

    Sufficed to say, I would definitely appreciate any help/insights offered.
    I've also attached a sample file if it helps.

    Thanks in advance!
    Attached Files Attached Files

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: How to find Second Mode across various columns

    I wrote a function that can do it.

    =ModeN(nth Mode, ranges)

    =ModeN(1.F2,I2,L2,O2,R2) - 1st mode
    =ModeN(2,F2,I2,L2,O2,R2) - 2nd mode, etc


    Please Login or Register  to view this content.
    How to install your new code
    1. Copy the Excel VBA code
    2. Select the workbook in which you want to store the Excel VBA code
    3. Press Alt+F11 to open the Visual Basic Editor
    4. Choose Insert > Module
    5. Edit > Paste the macro into the module that appeared
    6. Close the VBEditor
    7. Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)

    To run the Excel VBA code:
    1. Press Alt-F8 to open the macro list
    2. Select a macro in the list
    3. Click the Run button
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Forum Expert newdoverman's Avatar
    Join Date
    02-07-2013
    Location
    Port Dover, Ontario, Canada
    MS-Off Ver
    2010
    Posts
    10,330

    Re: How to find Second Mode across various columns

    What exactly are you trying to do? MODE gives the most frequent value in a range. If there isn't a most frequent value in the range, an error will be the result.
    <---------If you like someone's answer, click the star to the left of one of their posts to give them a reputation point for that answer.
    Ron W

  4. #4
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: How to find Second Mode across various columns

    Quote Originally Posted by daffodil11 View Post
    I wrote a function that can do it.
    I think that there could be many ways to interpret what is the correct result of this one, your function doesn't always give the results that I would expect, where as I expect that this array formula doesn't give the results you would expect.

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Compare the results of your code to the formula for rows 8 and 9 of the sample file, and also an array of 1,1,2,3,3 or similar, where 2 values appear twice in the array.

    I think that it might be best to wait for the OP to clarify before making too many changes.

  5. #5
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: How to find Second Mode across various columns

    I omitted a step which I should probably go back and figure out to make the result more consistent: reordering the array from highest to lowest value.
    Then simply pulling the nth index of that array would produce nth most occurring value even for ties.

    I have no clue how to reorder a Dictionary by value. =(

  6. #6
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: How to find Second Mode across various columns

    You have to reorder before passing it to the dictionary.

    Might be easier to create a 2d array, passing each unique value and count of that value, then indexing by rank of count.

  7. #7
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: How to find Second Mode across various columns

    Everyone,
    Thank you for your contributions!

    daffodil11,
    I've run your code and I believe I can make it work with the iferror function when a 2nd mode doesn't really exist (e.g., mode1 and mode2 are the same number), otherwise it will return an #VALUE! error in the cell I apply the function into.

    I really like this solution so far, and I also like that it defaults to the 1st value found (w.r.t. to left-to-right column order) for a secondary mode!

    The only hiccup I've found with this one so far (beyond the aforementioned) is that it has a hard time trying to determine anything beyond the 2nd mode, it seems to error out (#VALUE!), and I can't seem to use a simple iferror approach to tell it to pick one of the other remaining values.


    jason.b75,
    Not sure if I applied your formula correctly, but I appear to be getting inconsistent results, sometimes the 1st mode, other times the 2nd.
    Can you please elaborate on implementation, and also how to modify this for modes 3, 4, etc?
    Also, could you explain what's going on with the A1:A5 or A1:A6 range references?


    newdoverman,
    I need the 2nd and possibly 3rd, 4th, etc., modes as backups for when the Mode1 value isn't feasible.
    Basically, given a list of 5 non-contiguous values, the value that appears the most should be the closest match to fit my need.
    If however, that closest match doesn't work, then I need to reference the 2nd closest match as the alternative solution for consideration. Failing that, 3rd, 4th, and up to 5th mode values will need to be evaluated in those orders.

    My sample workbook only consisted of 13 or so rows, but I will need to do this for over 12,000 rows in the final workbook.
    Last edited by hamidxa; 08-31-2015 at 11:57 AM.

  8. #8
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: How to find Second Mode across various columns

    I don't think that the formula is a practical solution, but if you want to test it to compare results then it needs to be entered as an array formula.

    Copy the formula from my post end enter it into S2, but don't press enter.

    Hold down Shift and Ctrl, then press Enter to confirm the formula, if you check the formula bar, the formula should be encased in curly brackets.

    Copy the formula to the other rows and columns by holding the left mouse button down on the bottom right corner of the cell and dragging the fill handle.

  9. #9
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: How to find Second Mode across various columns

    Jason,

    Thanks for the explanation, your formulas seem to do the trick!
    Any reason why you feel that this is an impractical approach however -- processor intensive, or ...?

  10. #10
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: How to find Second Mode across various columns

    I haven't tested the formula with large volumes of data, only your sample and a couple of extra rows to test some data patterns that were cause for concern, so not sure about processor intensiveness, but that was one of my reasons for referring to the formula as impractical.

    The other would be ease of use, how many people would be able to follow the formula and change it if you needed to add another column?

    edit:- 3rd concern removed post research.
    Last edited by jason.b75; 08-31-2015 at 03:10 PM.

  11. #11
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: How to find Second Mode across various columns

    That formula is impressive. I tried a few variations incorporating FREQUENCY to get around the noncontiguous limitations of COUNTIF and MATCH.

    After like an hour I caved and started working on my first ever UDF. And then things got complicated. And terrible.

  12. #12
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: How to find Second Mode across various columns

    It took a billion years, but I found some code to sort the dictionary by Item value. Then I converted the code to Late Binding and descending order.

    In the event of a tie, it shows the lowest order key first. Given the following range A1:A10: {3,5,3,5,3,5,7,4,7,4}

    =ModeN(1,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10) = 3
    =ModeN(2,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10) = 5
    =ModeN(3,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10) = 4
    =ModeN(4,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10) = 7
    =ModeN(5,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10) = #VALUE!

    Please Login or Register  to view this content.
    That was hard stuff.

  13. #13
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: How to find Second Mode across various columns

    daffodil11 & jason.b75,
    Thank you both, these are both some really impressive solutions!

+ 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. How to Find Highest value from most repeated (MODE) values
    By Ihsan Tsani in forum Excel General
    Replies: 15
    Last Post: 09-29-2014, 03:36 AM
  2. Find mode by using subtotal feature?
    By sccrbrg in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-30-2014, 12:03 PM
  3. Find and Replace function in larger mode
    By HelpExc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-21-2012, 04:38 PM
  4. [SOLVED] Find the MODE of a column
    By Virtual_Jase in forum Excel General
    Replies: 8
    Last Post: 10-04-2012, 10:09 AM
  5. Replies: 12
    Last Post: 03-04-2011, 01:42 PM
  6. extend mode with Find Next
    By HGood in forum Excel General
    Replies: 1
    Last Post: 04-15-2005, 08:06 PM
  7. How do I find a mode while disregarding a specific cell value?
    By glasskit in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-25-2005, 04:06 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