Trusted Members
October 17, 2018
Trusted Members
Moderators
November 1, 2018
April 16, 2024
Many thanks for looking into my issue, my code is as follows:
Function GETCOLORCOUNT(CountRange As Range, CountColor As Range) As Single
Dim CountColorValue As Long
Dim TotalCount As Single
Dim rCell As Range
Application.Volatile
CountColorValue = CountColor.Interior.Color
Set rCell = CountRange
For Each rCell In CountRange
If rCell.DisplayFormat.Interior.Color = CountColorValue Then
TotalCount = TotalCount + 1
Else
End If
Next rCell
GETCOLORCOUNT = TotalCount
End Function
Trusted Members
Moderators
November 1, 2018
April 16, 2024
I was able to remove DisplayFormat from the function. The updated code gives the same, correct Formula Result as before in the Function Arguments window (which is 5), but it is now showing 0 in the cell.
I was unsure how to rewrite the code using COUNTIF. The updated code is as follows:
Function GETCOLORCOUNT(CountRange As Range, CountColor As Range) As Single
Dim CountColorValue As Long
Dim TotalCount As Single
Dim rCell As Range
Application.Volatile
CountColorValue = CountColor.Interior.Color
Set rCell = CountRange
For Each rCell In CountRange
If rCell.Interior.Color = CountColorValue Then
TotalCount = TotalCount + 1
Else
End If
Next rCell
GETCOLORCOUNT = TotalCount
End Function
Trusted Members
Moderators
November 1, 2018
April 16, 2024
The cells that are being counted are coloured using Conditional Formatting. I have removed this and applied the colouring manually, and the function now works, amazing!
I assume I should work on rewriting the code using Countifs, unless you have any other suggestions?
Thank you so much for your help, if this function works it will save us a huge amount of time. I work in a secondary school and we are currently doing the colour counting using VBA code that is run using a button. But on some of our spreadsheets, which are used by all our teachers for reporting assessment results to our students, the code takes a long time to run.
Trusted Members
Moderators
November 1, 2018
April 16, 2024
Many thanks for your reply, apologies for not getting back to you sooner, it has been a very busy week.
I have just tried to work out how to do this using COUNTIF but have not got anywhere with it. Are you able to send me an example formula?
The following users say thank you to Karla de Visser for this useful post:
VelouriaTrusted Members
Moderators
November 1, 2018
Trusted Members
Moderators
November 1, 2018
For the green count:
=SUMPRODUCT(--(O13:s13/O$7:S$7>=0.75))
For the red (first):
=SUMPRODUCT(--(O13:s13/O$7:S$<0.5))
For the balance, the simplest option is just to count the numbers in the range - COUNT(O13:S13) - and then subtract the results of the other two calculations.
Answers Post
1 Guest(s)