- Thank you received: 9
Welcome to the fracta.net forum!
Share your coding ideas or ask questions.
Share your coding ideas or ask questions.
Question Excel function that counts all cells with a specific color
12 years 11 months ago - 12 years 4 months ago #24
by roller
Excel function that counts all cells with a specific color was created by roller
I had few people ask me if there is a way in Excel 2007 to count cells with a certain colour. Excel 2007 doesn't have a build in function to do this - Excel 2007 has functions to count cells with numbers, cells that are not blank and some other count functions.
But you can easily add this functionality to your Excel book by writing your own custom function.
Start by bringing up the VBA editor (ALT+ F11) and in a new module add a macro and copy the code below. Then in an Excel book on sheet 1 enter the function like you would with any built in functions like this =countColor(range to count, cells with colour to count). You should see this new function (countColor) appears in the list of available functions.
How to use it:
"Range to count" is simply the cells you want to count the desired colour, simply drag the mouse over the range you want to count.
"Cells with colour to count", simply select one cells that contains the colour you want to count.
So the function will look something like this =countColor(B3:T3,$I$5), where B3 to T3 the range to count the colour and cell I5 contains a sample of the colour to count.
The code:
Function countColor(r As Range, c As Range) As Integer
Application.Volatile
Dim rr As Range
Dim tot As Integer
col = c.Interior.Color
For Each rr In r
If rr.Interior.Color = col Then tot = tot + 1
Next rr
countColor = tot
End Function
Finally to make this run properly we need to trigger this macro somehow, what I did is added the below code to the Sheet 1 module in the VBA editor. This will make Excel calculate every time you click on different cells causing our function to update.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.Calculate
End Sub
But you can easily add this functionality to your Excel book by writing your own custom function.
Start by bringing up the VBA editor (ALT+ F11) and in a new module add a macro and copy the code below. Then in an Excel book on sheet 1 enter the function like you would with any built in functions like this =countColor(range to count, cells with colour to count). You should see this new function (countColor) appears in the list of available functions.
How to use it:
"Range to count" is simply the cells you want to count the desired colour, simply drag the mouse over the range you want to count.
"Cells with colour to count", simply select one cells that contains the colour you want to count.
So the function will look something like this =countColor(B3:T3,$I$5), where B3 to T3 the range to count the colour and cell I5 contains a sample of the colour to count.
The code:
Function countColor(r As Range, c As Range) As Integer
Application.Volatile
Dim rr As Range
Dim tot As Integer
col = c.Interior.Color
For Each rr In r
If rr.Interior.Color = col Then tot = tot + 1
Next rr
countColor = tot
End Function
Finally to make this run properly we need to trigger this macro somehow, what I did is added the below code to the Sheet 1 module in the VBA editor. This will make Excel calculate every time you click on different cells causing our function to update.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.Calculate
End Sub
Last edit: 12 years 4 months ago by roller.
Please Log in or Create an account to join the conversation.
- drm51
- Visitor
11 years 7 months ago #138
by drm51
Replied by drm51 on topic Re: Excel function that counts all cells with a specific color
I am using this function to count colors in an Excel workbook, but I have 6 sheets that I want to count color cells in. With the trigger mechanism:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.Calculate
End Sub
it only allows me to count colored cells in sheet 1 and not the others. What do I need to change to allow it to operate throughtout all sheets?
Thanks for your help.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.Calculate
End Sub
it only allows me to count colored cells in sheet 1 and not the others. What do I need to change to allow it to operate throughtout all sheets?
Thanks for your help.
Please Log in or Create an account to join the conversation.
11 years 6 months ago #139
by roller
Replied by roller on topic Re: Excel function that counts all cells with a specific color
Try adding the below code to each of the sheets module:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.Calculate
End Sub
Or even try puting the below code in the Workbook module instead of each of the sheets:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Application.Calculate
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.Calculate
End Sub
Or even try puting the below code in the Workbook module instead of each of the sheets:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Application.Calculate
End Sub
Please Log in or Create an account to join the conversation.
- drm51
- Visitor
11 years 6 months ago #140
by drm51
Replied by drm51 on topic Re: Excel function that counts all cells with a specific color
Thanks for help, it works great now.
Please Log in or Create an account to join the conversation.
Time to create page: 0.870 seconds