Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to count the cells with text

Status
Not open for further replies.

wmbb

Technical User
Jul 17, 2005
320
NL
I want to count the cells with text in red and black in an excel-sheet using the user defined function below:

Function FntColor(rng As Range) As String
If rng.Font.Color = RGB(0, 0, 0) Then
FntColor = "black"
ElseIf rng.Font.Color = RGB(255, 0, 0) Then
FntColor = "red"
Else: FntColor = "normal"
End If
End Function


What do I have to do to get it working in:
=COUNTIF(B5:B10,FntColor() = "black")
 


Your FontColor function requires an argument range reference. It has nothing to do with the COUNTIF function.

If you wanted to use the COUNTIF function, you would FIRST need to use the FontColor function in anothe column to return a string. THEN do a COUNTIF on THAT column to count string occurences of whatever.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Is it possible to do it all in VBA ?
Counting the cells in the range B5:C10 containing black text.

Suggestions for some code ?
 
Loop over the range and for each cell add 1 to a counter is the color is black...
There might be smarter ways, but this should work.
 
Thnx for your response.

I'm no expert so I need some help....
What is the code to loop over the range B5:C10

Thnx in advance
 



Have you SEARCHED this site as suggested?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I think I missed the suggestion, but I always search the site first.

I didn't recognize anything I could try or use.
Do you know a link to post I could use ?
 
Thnx for your quick response
 
Could be like this:

Code:
Sub countblacktext()
  Dim counter As Integer
  counter = 0
  For Each cell In Range("A1:B2")
    If cell.Font.Color = 0 Then
      counter = counter + 1
    End If
  Next
  MsgBox ("There are " & counter & " cells with black text!")
End Sub
 
For use on a spreadheet:

Code:
Sub countblacktext(rng as range)
  Dim counter As Integer
  counter = 0
  For Each cell In rng
    If cell.Font.Color = 0 Then
      counter = counter + 1
    End If
  Next
CountBlackText = counter
End Sub
Called using

=CountBlackText(B5:C10)

If you had done a search for "Sum by colour" as I suggested in your original MSOffice thread, you would've found something very similar - you could've just changed the sum to a counter...

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
The code below is working for me.

Function act_saldo() As Integer
act_saldo = 0
For Each Cell In Range("B5:C10")
If Cell.Font.Color = RGB(0, 0, 0) Then
act_saldo = act_saldo + 1
End If
Next
End Function


I filled a cell with "=act_saldo()"
This results in the correct number of cells with black text.

But...
...when I change the font color of cells in the range, the value of the cell with the function does not change until the cell is selected and enter is pressed.
Is there a way to do this automatically.

(Automatic calculation is turned on !)
 



Add

Application.volatile

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top