Counting non-empty cells with a set range of values of a certain font colour

NZWardy

New Member
Joined
Nov 7, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have been going around in circles trying to get this to work as only have a mild understanding of VBA. I have a large spreadsheet for our staff work allocation and when they are on jobs/training or spare they are in a specific font colour.
Each column is a separate day and at the bottom I want to automatically count the number of spare resources which ends up in a graph. I am using =CountColour(DD12:DD101,$A$120) in this cell.

My VBA code is currently this;
Code:
Public Function CountColour(pRange1 As Range, pRange2 As Range) As Double
Application.Volatile
Dim rng As Range
Dim stafflist As Range
Dim foundCell As Range
On Error Resume Next
Set stafflist = Sheet31.Range("B2:36")
    For Each rng In pRange1
    If rng <> "" Then
    Set foundCell = stafflist.Find(What:=rng.Value, LookIn:=pRange1, LookAt:=xlPart)
    If foundCell <> "" And rng.Font.Color = pRange2.Font.Color Then
    CountColour = CountColour + 1
    End If
    End If
Next
End Function

What am I missing?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I am not sure about what you ask for and let me share this one. For the range, using
"If Not rng Is Nothing Then" "If Not foundCell is Nothing Then" is better than "If rng <> "" Then"
 
Upvote 0
I am not sure about what you ask for and let me share this one. For the range, using
"If Not rng Is Nothing Then" "If Not foundCell is Nothing Then" is better than "If rng <> "" Then"
Ok, thanks for that, have edited it to now read;
VBA Code:
Public Function CountColour(pRange1 As Range, pRange2 As Range) As Double
Application.Volatile
Dim rng As Range
Dim stafflist As Range
Dim foundCell As Range
On Error Resume Next
Set stafflist = Sheet31.Range("B2:37")  
    For Each rng In pRange1
    If Not rng Is Nothing Then
    Set foundCell = stafflist.Find(What:=rng.Value, LookIn:=pRange1, LookAt:=xlPart)
    If Not foundCell Is Nothing Then
    If rng.Font.Color = pRange2.Font.Color Then
    CountColour = CountColour + 1
    End If
    End If
    End If
Next
End Function

Still doesn't count the spare resource though. Every cell is resulting in 0.
Here are 2 pics of the main sheet and the reference range
1667938779989.png
1667938818780.png
1667938874783.png
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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
Back
Top