+ Reply to Thread
Results 1 to 9 of 9

Formula to find majority value with Excel/VBA

  1. #1
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Question Formula to find majority value with Excel/VBA

    Hi Gurus,

    Would need your kind expertise to help on this request.

    Assuming I’ve the following sample data (see attached) and wishing if the expected output can be generated using some kind of excel/vba formulas.

    Sample output data in Column E:
    Student2 -> Found majority values in Column D -> Returned
    Student3 -> Found majority values in Column D -> Hold
    Student5 -> Found majority values in Column D -> Returned

    If no majority values present when checked, nothing will be returned.

    The sample data could be long and a lot, so it's best if the formula can be made flexible to cater for this situation.

    Appreciate for any of your help and advice.

    Thank you a lot in advance.
    Attached Files Attached Files
    Regards,
    Jack

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,684

    Re: Formula to find majority value with Excel/VBA

    Do you only have Returned or Hold in column D?

    Pete

  3. #3
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,684

    Re: Formula to find majority value with Excel/VBA

    I used 2 helper columns, to keep the formulae simple:

    G1: =IF(COUNTIF(A$1:A1,A1)=1,COUNTIFS(A:A,A1,D:D,"Returned"),"")

    H1: =IF(COUNTIF(A$1:A1,A1)=1,COUNTIFS(A:A,A1,D:D,"Hold"),"")

    Copy these down to the bottom of your data, to give a count of each for each student, but only on the first row where the student name appears.

    Then you can use this formula in F1:

    =IF(OR(G1="",G1=H1),"",IF(G1>H1,"Returned","Hold"))

    and copy down - you can compare it directly with your entries in column E.

    Hope this helps.

    Pete

  4. #4
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Formula to find majority value with Excel/VBA

    Hi Pete_UK,

    Thanks for your reply and help.

    Forgot to mention there are only 2 values for Column D, either Returned or Hold.

    Possible to simplify the formula into 1 single line? It'll be easier to maintain.

    Thanks again.

  5. #5
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,684

    Re: Formula to find majority value with Excel/VBA

    I don't think it will be easier to maintain if it were combined into one formula. Essentially, you could put the formula from G1 into the formula in F1 wherever G1 appears, and the same for the formula in H1, but that means 5 substitutions would need to be made and there would be 3 lots of expression relating to the G1 term and 2 duplicates relating to H1. The formula would be much longer as well as having those duplicated expressions, so it would be MORE difficult to maintain. In addition, though, the formulae in G1 and H1 contain IF functions, so you would need to be careful to ensure that all branches of the IF are catered for.

    I think I have given you the most efficient approach, and each (smaller) formula can be easily maintained.

    Pete

  6. #6
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Formula to find majority value with Excel/VBA

    Hi Pete_UK,

    Thanks for your reply and help.

    I've tested the formula and noticed some issues.

    Maybe I need to highlight that the formula should check for the following case, see below:
    If all raws in "Column D" of Student6 has a status indicated as "Returned", then the formula should return "Returned".
    If all raws in "Column D" of Student6 has a status indicated as "Hold", then the formula should return "Hold".
    However, if all raws in "Column D" of Student6 has a status as mixture of "Returned" and "Hold", then the formula should not return anything.

    I've attached the updated sample for your reference.

    Appreciate if could help to check.

    Thanks.
    Attached Files Attached Files

  7. #7
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,684

    Re: Formula to find majority value with Excel/VBA

    Change the formula in J1 to this:

    =IF(OR(G1="",G1=H1,AND(G1>0,H1>0)),"",IF(G1>H1,"Returned","Hold"))

    then copy down.

    Hope this helps.

    Pete

  8. #8
    Forum Contributor
    Join Date
    12-30-2006
    Location
    Australia
    MS-Off Ver
    2017
    Posts
    151

    Re: Formula to find majority value with Excel/VBA

    Hi Pete,

    Thanks so much, it's working as expected now.

    Will close the thread when it's been finalized.

    Have a nice day.

  9. #9
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,684

    Re: Formula to find majority value with Excel/VBA

    Glad to help, Jack - thanks for the rep.

    Pete

+ 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. Calculating a majority value
    By legoman26 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-01-2015, 06:03 PM
  2. Listing the Matching Majority in a Related Column
    By Rschwar23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-11-2014, 02:19 PM
  3. Return The Majority Value
    By sinspawn56 in forum Excel General
    Replies: 6
    Last Post: 10-07-2009, 11:15 AM
  4. Return majority value in cell
    By asdvender in forum Excel General
    Replies: 6
    Last Post: 10-17-2008, 09:59 PM
  5. Formula to calculate majority value
    By melanemac in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-20-2006, 01:07 PM
  6. How do I automate a majority rules?
    By Yankee1548 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-05-2006, 12:00 PM

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