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

Michaelk7

New Member
Joined
Oct 24, 2011
Messages
4
Note: I've already posted this in another forum:http://www.vbaexpress.com/forum/showthread.php?t=39544
but after hours of waiting I didn't get any reply, and I need this urgently, so can someone pls help:

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
Ps I only want the vba sub for the solution not the worksheet functions
Can anyone help pls?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Michaelk7

New Member
Joined
Oct 24, 2011
Messages
4
What I've done so far:
Code:
Sub Button1_Click()
 Dim rfound As Range
 Dim lCount As Long
 Set rfound = Range("B1")
 
 For lCount = 1 To WorksheetFunction.CountIf(Columns(2), "RaceA")
 Set rfound = Columns(2).Find("RaceA", After:=rfound, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False)
With rfound
'code to search for the corresponding Time and then identify the ranking
End With
Next lCount
End Sub
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 !
 

Watch MrExcel Video

Forum statistics

Threads
1,130,303
Messages
5,641,426
Members
417,209
Latest member
Agbarker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top