+ Reply to Thread
Results 1 to 6 of 6

VBA code to find highest and 2nd highest number based in criteria

  1. #1
    Registered User
    Join Date
    10-16-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    28

    VBA code to find highest and 2nd highest number based in criteria

    Please note firstly I've post this on different forum but didn't get any solution after many hours of waiting, so can someone help me out here please. Where I've posted: here and here and here

    Problem:
    I need the VBA code to find the highest and 2nd highest value in a column based on criteria in another column. So for example:
    Type | Time
    RaceA| 4.5
    RaceB| 5.5
    RaceA| 6.2
    RaceA| 3.1
    RaceB| 2.1
    I need the VBA code to be able to find the highest and 2nd highest Time for RaceA and highlight them in different color. So in the example above, the code should loop through the time based on Type and highlight 3.1 as highest and 4.5 as second highest
    I need the vba sub instead of worksheet function.
    Can anyone help pls?
    Last edited by Michael007; 10-25-2011 at 08:15 AM.

  2. #2
    Registered User
    Join Date
    10-16-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: VBA code to find highest and 2nd highest number based in criteria

    What I've done so far:
    Please Login or Register  to view this content.
    However I'm unsure on how to search for the corresponding time for Race A, and then give it a ranking of 1st, 2nd and 3rd. Can someone please help !

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA code to find highest and 2nd highest number based in criteria

    Try this code - Green color for 1st highest, Yellow for 2nd highest. The concept here is that you sort the data on the basis of Race # as well as the timings from smallest to largest. This way, the 1st 2 numbers will always be the 1st and 2nd highest respectively.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-16-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: VBA code to find highest and 2nd highest number based in criteria

    Hi thanks for the code. I tried to run it just now and it does work. But I've seen that you have also sorted RaceB, however I only want RaceA to be sorted and is there a way I can add a 3rd highest time?

    As you can see from the attachment, I've attach my worksheet
    Sorry if I didn't made it clear at start
    Attached Files Attached Files
    Last edited by Michael007; 10-25-2011 at 07:32 AM.

  5. #5
    Registered User
    Join Date
    10-16-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: VBA code to find highest and 2nd highest number based in criteria

    Also I'll attach the worksheet after I run the macro, as you can see in the worksheet, the fastest time should be 62.88 but instead it highlights 63.496
    Attached Files Attached Files

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA code to find highest and 2nd highest number based in criteria

    In the example in your first post, you should have specified all the columns. Change the code as follows - (You have changed most of it)
    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)

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