Home > Net >  Is my code right or am I missing something?
Is my code right or am I missing something?

Time:05-20

Trying to go through some cells in excel that if it finds 2 cell backgrounds in that RGB color then it would show a message. That is what I could think of and so far it is not working. Can someone tell me if it makes sense or has a better idea?

Dim counter As Integer
Dim cell As Range
Dim total As Integer
total = 0

For Each cell In Worksheets("Sheet1").Range("B58:B61").Cells
    If cell.Interior.Color = RGB(218, 150, 148) Then
        total = total   1
        If total = 2 Then
            MsgBoxStop.Show
        End If
    End If
Next cell

CodePudding user response:

If you want to show a message after find two cells with a RGB(218,150,148), you need to replace MsgBoxStop.Show to MsgBox "your message", vbInformation, "Your title

If total = 2 Then MsgBox "2 RGB(218,150,148) has be found", vbInformation, "TestCode" cell.activate Exit For End If

CodePudding user response:

So it seems that when using Conditional Formatting in excel, you cant use (cell.Interior.Color) you would have to use (cell.DisplayFormat.Interior.Color).

My code works perfectly now!

     Dim counter As Integer
     Dim cell As Range
     counter = 0

     For Each cell In Range("B60:B63")
         If cell.DisplayFormat.Interior.Color <> RGB(255, 255, 255) Then
             counter = counter   1
             If counter = 2 Then
                  MsgBox "Stop!"
             End If
         End If
     Next cell
  • Related