Counting cells with a certain font colour

N

NDBC

I have a row of lap times which represents the lap times of the two
competitors in a team. The competitors do not have to do alternative laps.
Either of the two riders can do any lap in random order. My aim is to
identify which rider did each lap and then give an average lap time for each
rider based on the laps they completed.


Each rider in the team is identified by an indivual tag that is scanned on
the completion of each lap so it is easy to identify which rider did each lap.

This is my thoughts on how to do it. Using code I was going to record the
lap times of rider1 in say blue font and the lap times of rider 2 in red
font. This immediately gives a visual cue to the riders who did what laps.
Then I was going to count and add the cells of each font colour to calculate
the average.

Based on my searches of this forum I think this could be done but can anyone
think of an easier way.

Below is an example of a typical team result setout where Av is the average
and R1 represents a lap time by rider 1 and R2 by rider2.


R1Av R2Av R1 R1 R2 R1 R1 R1
R2 R1 R2
 
N

NDBC

In case it is important there would be around 100 teams and a maximum of
around 20 laps completed by any one team. The races are based on a 4 hour
time duration not set number of laps so the number of laps is unknown at the
start of each race.
 
P

Patrick Molloy

Its quite easy to do your way, and as you said, its quite visually obvious.

to start color a number of cells , and range name them rider1, rider2 and so
on

add this code to a standard code module, its a UDF (iser defined function)

Option Explicit

Function GetAverage(source As Range, rider As Long) As Double
Dim clr As Long
Dim total As Long
Dim count As Long
Dim cell As Range
clr = Range("rider" & rider).Interior.Color
Application.Volatile
For Each cell In source.Cells
If cell.Interior.Color = clr Then
count = count + 1
total = total + cell.Value
End If
Next

If count > 0 Then
GetAverage = total / count
Else
GetAverage = 0
End If
End Function


in the worksheet, say cells C6 to K6 i have colred and added values
in B6 put this
=GetAverage(C6:K6,1)

this says check each cell in the selected range and give me the average for
rider #1
In A6 put this
=GetAverage(C6:K6,2)
which references the same row of times, but returns the average for rider #2

its versatile - you can change colors and add riders without having to amend
to code


file
http://cid-b8e56c9a5f311cb7.skydriv...lic/Excel Files/ColorAverage/colorAverage.xls
 
P

Patrick Molloy

i used cell color instead of font color

but this is an easy change
replace
..Interior.Color
by
..Font.Color
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top