
The requirement is a piece of code which can count instances of a string i.e(ABC,DEF,GHK), based on if they are present in a colored cell or not and put the results in a cell below as shown .
Can anyone please advise?
I tried a sample code
Sub Color()
Dim varCounter As String
Dim color As Integer
Dim nocolor As Integer
Range("E5").Select
color= 0
nocolor= 0
Do Until Selection.Value = ""
If Selection.Font.Color = RGB(255, 0, 0) Then
color= color+ 1
Else
nocolor= nocolor+ 1
End If
Selection.Offset(1, 0).Select
Loop
Range("E47").Select
Selection.Value = no
Range("E48").Select
Selection.Value = color
End Sub
This a very simple code which check if the text font is colored or not , buut I couldn’t find anything which check for the background color of the cell.
I also tried excel formula , but that with that I can only search for text and count , it doesn’t count based on background color of the cell.
Here’s a simple user-defined function. You can put it in a regular module. You can then call it from any worksheet in the workbook it’s in:
It takes three arguments: the range to evaluate, the string to search for and whether you’re counting colored (or uncolored) cells:
So, above, in column E the formula is:
In column F it’s the same except the last argument,
CountColoredisTRUE.I don’t write many user-defined functions, so somebody may come along and point out problems or improvements.