Color Detection in Excel via VBA

Solved
Kilyox -  
 Kilyox -
Hello,
I would like to know how to detect if there is a red cell (for example) in an Excel sheet and, if so, increment a counter with the number of cells found.
I can only detect if the selected cell is red or not, but that’s just a matter of having eyes...
I don’t know VBA very well, but I’m quite good with VB.
Could you please help me?!
Thanks in advance
Configuration: Windows XP Firefox 3.0.1

4 answers

  1. anocheda Posted messages 55 Status Member 3
     
    Houhou!!!!1 my first VBA code

    Sub RedColor()
    Dim i, j, n As Integer

    n=0
    For i = 1 To 10
    For j = 1 To 10
    If Cells(i, j).Interior.ColorIndex = 3 Then n = n + 1
    Next j
    Next i

    Cells(1, 1) = n

    End Sub

    It counts the number of red cells between row 10 and column 10
    you can change the search size by increasing i and j

    it gives the result in A1 you can change that
    if you want to change the color 3 it's red, the rest I don't know
    0
  2. lermite222 Posted messages 9042 Status Contributor 1 199
     
    Hello,
    to get the list of colorIndex, press F1 and in the search box type PatternColorIndex, property
    You'll get the page with the colors and their numbers.
    See you later
    --
    Experience teaches more surely than advice. (André Gide)
    0
  3. lermite222 Posted messages 9042 Status Contributor 1 199
     
    To search for color...
    Sub SearchColor() Dim Color As Integer Dim Cell As Range Dim Counter As Integer Color = 3 Sheets("Sheet1").Select ' adjust to the sheet name For Each Cell In Range("A1:J10") ' adjust the range to the cells to test If Cell.Interior.ColorIndex = Color Then Counter = Counter + 1 Next MsgBox "There are " & Counter & " cells with color No. " & Color End Sub

    See you later
    --
    Experience teaches more surely than advice. (André Gide)
    0
  4. Kilyox
     
    Thank you very much, you are very efficient!
    I wasn't far off but still had to search :)
    0