1

sample excel

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.

3 Answers 3

3

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:

Public Function CountByColorAndText(rng As Excel.Range, SearchText As String, CountColored As Boolean) As Long
Dim cell As Excel.Range
Dim CellCount As Long

For Each cell In rng
    If cell.Value = SearchText Then
         If (cell.Interior.ColorIndex = -4142 And Not CountColored) Or _
           (cell.Interior.ColorIndex <> -4142 And CountColored) Then
            CellCount = CellCount + 1
        End If
    End If
Next cell
CountByColorAndText = CellCount
End Function

It takes three arguments: the range to evaluate, the string to search for and whether you're counting colored (or uncolored) cells:

enter image description here

So, above, in column E the formula is:

=CountByColorAndText($A$2:$A$13,$D3,FALSE)

In column F it's the same except the last argument, CountColored is TRUE.

I don't write many user-defined functions, so somebody may come along and point out problems or improvements.

6
  • Mate I'm getting the folowing error #NAME? , when I apply the same formula . ANy advise?
    – misguided
    Jan 9, 2013 at 23:27
  • That error means that it doesn't recognize the function. Did you paste it into a module and call it exactly as described in my answer? Jan 9, 2013 at 23:40
  • Works now, thanks.I am accepting your answer :) . The logic might have some issues though , the color count is not showing correct for few searched texts :(
    – misguided
    Jan 10, 2013 at 0:13
  • I,m using excel 2003, not sure if it warrants any change in logic?
    – misguided
    Jan 10, 2013 at 0:19
  • I don't think so. So in some cases it doesn't count correctly and in some it does? If that's the case, make sure you're selecting the correct range. This might be an issue if the range isn't absolute and then you drag it. Jan 10, 2013 at 0:21
2

Instead of Font.Color use Interior.Color

2
  • Mate I'm not sure of the RGB value for the color I'm using , so don't know how to compare that one using Interior.Color. For example the RGB value for red is 255,0,0
    – misguided
    Jan 9, 2013 at 23:29
  • This site has a list of all the standard Excel palette colours and their colour numbers. Jan 10, 2013 at 0:38
0

Writing a code again would be great. But I have A REVISED CODE here one if you are interested to take a look.. I am just wondering if this is some commmon project though, coz yesterday the OP asks the very same question....

VBA, COUNTIF, Exclude based on cell color

BTW INTERIOR.COLOR will show you a big number that's a representation of RGB, where as you might want to use INTERIOR.COLORINDEX

Since you are checking RGB format, you may try the following. However I recommend you to not to use select, it slows down your code. You may change sheets, ranges according to yours needs.

e.g.

Dim rng as Range
Dim cell as Range

'-- name column
Set rng = Sheets(2).Cells(Sheets(2).Rows.Count, "C").End(xlUp).Row

color= 0
nocolor= 0

 For Each Cell In rng
    If  Cell.InteriorColor = RGB(256,0,0) then
      color= color+ 1
    Else
      nocolor= nocolor+ 1
    End If
  Next Cell

'--output
Sheets(2).Range("E47").Value = nocolor
Sheets(2).Range("E48").Value = color
3
  • @misguided please take a look at the post and comment. If you require further help do let us know :)
    – bonCodigo
    Jan 9, 2013 at 7:52
  • sorry mate , I'm new to this . ACtually I dnt know the RBG value to the color to compare it to . ALso basically if the code would have been little generic , i.e it counts colors irrespective of which color would have made it easier. PS: No I'm not working with the other guys (who posted the other question ). Might be a coincidence :)
    – misguided
    Jan 9, 2013 at 23:35
  • @misguided this code is generic infact, with a small change. Using an UDF has quite some downside as when one cell fires, everyone else fires too... So imagine if you have 10 cells with colours and when you add UDF in each cell... Here is something to read on UDF performance and behaviour. I have added an editted version to get the table that you need! In any programme performance and maintainability is something very important. E.g. Do you want to change 10 places or do you want to change one place? ;)
    – bonCodigo
    Jan 10, 2013 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.