+ Reply to Thread
Results 1 to 15 of 15

Highlight and order most popular values

  1. #1
    Registered User
    Join Date
    01-24-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    9

    Highlight and order most popular values

    Hello there VBA experts,

    I have a list of faults and each fault has a specific fault code e.g. Hardware failure = fault code 101

    There are only a set amount of fault codes in the data I extracted roughly around 10. What I would like to do is organise the data based on the fault code and highlight the whole row red for the fault code that appears the most times. So if fault 101 appeared the most times that month the data would be organised with all fault 101's at the top and then decline with the fault codes that appear less and less. I think it would be pretty cool if the data could be organised like this with the most popular faults highlighted in red at the top and then correspond down to the faults that maybe have one occurance and highlight them in green or amber etc.

    Any tips or starting points with this VBA macro would be most welcomed !

    Thanks

    HB

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Highlight and order most popular values

    You could accomplish this using countif and conditional formatting. In the attachment I have created a random set of fault numbers (1-10) and assigned them values (A-I) >> calculated the occurrence of each fault value using Countif >> sorted the worksheet in the hierarchy Occurrence then Fault value >> added conditional formatting.

    Have a look at the attachment.

    abousetta
    Attached Files Attached Files
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Registered User
    Join Date
    01-24-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Highlight and order most popular values

    Thanks for the quick response !

    This is brilliant ! exactly what I was after, Would I be able to see your code for the above spreadsheet if possible.

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Highlight and order most popular values

    Try this:-
    This code asumes you Fault codes are in column "A" and you have 6 columns of Data.
    Results start "K1".
    Please Login or Register  to view this content.
    Regards Mick

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Highlight and order most popular values

    I didn't use any code in the example. It's all the Countif formula and conditional formatting. Attach a sample workbook if you are having trouble replicating it.

    abousetta

  6. #6
    Registered User
    Join Date
    01-24-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Highlight and order most popular values

    Thanks Mick !
    That worked perfectly, very impressive peice of coding there

    How would I change the colour for each fault code

  7. #7
    Registered User
    Join Date
    01-24-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Highlight and order most popular values

    In addition Could I replace the current data with the new organised data rather then having it start at k1 ?

    Quote Originally Posted by Harrison_VBA View Post
    Thanks Mick !
    That worked perfectly, very impressive peice of coding there

    How would I change the colour for each fault code

  8. #8
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Highlight and order most popular values

    This will allow a header Row(1) then the code will reorganizre the data below, with "Fault Number" column = "A"
    See remarks in code for colours.
    Please Login or Register  to view this content.
    If you don't know the new colour codes you want:-
    Colours cells in new sheet "A1:A10", then run code for "Colour Index" numbers in "B"
    Please Login or Register  to view this content.
    Regards Mick

  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Highlight and order most popular values

    Hi Mick,

    You should be able to reduce:

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    Also bubble sort works fine as long as the list is not long, other methods are more efficient if the OP's list runs into the thousands (http://www.xtremevbtalk.com/showthread.php?t=78889).

    Hope this helps.

    abousetta

  10. #10
    Registered User
    Join Date
    01-24-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Highlight and order most popular values

    looks like when the +10 is removed to allow the original columns to be organised we are getting different numbers falling in the same category breaking the code

  11. #11
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Highlight and order most popular values

    I thought that might happen posssibe!!
    Try this:-
    Please Login or Register  to view this content.
    Regards Mick

  12. #12
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Highlight and order most popular values

    abousetta , Thank you for your comments and the Links, I'm sure ther will be useful.
    I used the code as below because I also wanted to capture the Individual ranges for each Unique as shown in red.
    [code]
    For Each Dn In Rng
    If Not .Exists(Dn.Value) Then
    .Add Dn.Value, Array(Dn, 1)
    Else
    Q = .Item(Dn.Value)
    Q(1) = Q(1) + 1
    Set Q(0) = Union(Q(0), Dn) .Item(Dn.Value) = Q
    End If
    Next
    /code]
    Regrds Mick

  13. #13
    Registered User
    Join Date
    01-24-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Highlight and order most popular values

    Unfortunately that still breaks it

    Quote Originally Posted by MickG View Post
    I thought that might happen posssibe!!
    Try this:-
    Please Login or Register  to view this content.
    Regards Mick

  14. #14
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Highlight and order most popular values

    Replace the last line as below:-
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    01-24-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Highlight and order most popular values

    I think thats a OT SOLVED ! Thanks for all your help Mick

    Quote Originally Posted by MickG View Post
    Replace the last line as below:-
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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