Find consecutive cells with specific cell formatting.

katana_flyer

New Member
Joined
Feb 19, 2020
Messages
17
Office Version
  1. 2013
Platform
  1. Windows
Looking for a way to search for 3 consecutive cells within the whole sheet, either vertically, horizontally or diagonally, with same cell formatting. ie. If cell a1,a2, a3 Or cells a1,b1,c1 Or cells a1,b2, c3 all receive red fill, then a msgBox. Displays.

Many thanks
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try this

VBA Code:
Sub FindColors()
  Dim c As Range, cad As String
  On Error Resume Next
  For Each c In ActiveSheet.UsedRange
    If c.Interior.Color = vbRed And c.Offset(0, 1).Interior.Color = vbRed And c.Offset(0, 2).Interior.Color = vbRed Then
      cad = c.Address & " " & c.Offset(0, 1).Address & " " & c.Offset(0, 2).Address
    End If
    If c.Interior.Color = vbRed And c.Offset(1, 1).Interior.Color = vbRed And c.Offset(2, 2).Interior.Color = vbRed Then
      cad = c.Address & " " & c.Offset(1, 1).Address & " " & c.Offset(2, 2).Address
    End If
    If c.Interior.Color = vbRed And c.Offset(1, 0).Interior.Color = vbRed And c.Offset(2, 0).Interior.Color = vbRed Then
      cad = c.Address & " " & c.Offset(1, 0).Address & " " & c.Offset(2, 0).Address
    End If
    If c.Interior.Color = vbRed And c.Offset(-1, 1).Interior.Color = vbRed And c.Offset(-2, 2).Interior.Color = vbRed Then
      cad = c.Address & " " & c.Offset(-1, 1).Address & " " & c.Offset(-2, 2).Address
    End If
    If cad <> "" Then
      MsgBox "Consecutive cells  : " & Replace(cad, "$", "")
      Exit Sub
    End If
  Next
End Sub
 
Upvote 0
Thank you DanteAmor, that did the trick.... almost , I had to change a few things to make it work the way I want. Now I ran into a wee problem.

This code works perfectly fine - once it finds 5 cells in a row diagonally with the either red or green cells, it highlights them all green ~ Just the way I want it to.

VBA Code:
Dim c As Range, cad As String
On Error Resume Next
For Each c In ActiveSheet.UsedRange

' Check for Diagonal Row from top left to bottom right and Color green
If (c.Interior.Color = vbRed Or c.Interior.Color = vbGreen) And (c.Offset(1, 1).Interior.Color = vbRed Or c.Offset(1, 1).Interior.Color = vbGreen) And (c.Offset(2, 2).Interior.Color = vbRed Or c.Offset(2, 2).Interior.Color = vbGreen) And (c.Offset(3, 3).Interior.Color = vbRed Or c.Offset(3, 3).Interior.Color = vbGreen) And (c.Offset(4, 4).Interior.Color = vbRed Or c.Offset(4, 4).Interior.Color = vbGreen) Then
c.Interior.Color = RGB(0, 255, 0)
c.Offset(1, 1).Interior.Color = RGB(0, 255, 0)
c.Offset(2, 2).Interior.Color = RGB(0, 255, 0)
c.Offset(3, 3).Interior.Color = RGB(0, 255, 0)
c.Offset(4, 4).Interior.Color = RGB(0, 255, 0)

End If
Next

but as soon as I add in this code, to search for 5 consecutive green or red cells diagonally from top right to bottom left it decides to fill in range a1 to d57 with green cells. Even if there is only 1 cell anywhere on the sheet coloured red or green. Any insight ? Thanks

VBA Code:
' check for diagonal row from top right down to top left and color green **
If (c.Interior.Color = vbRed Or c.Interior.Color = vbGreen) And (c.Offset(1, -1).Interior.Color = vbRed Or c.Offset(1, -1).Interior.Color = vbGreen) And (c.Offset(2, -2).Interior.Color = vbRed Or c.Offset(2, -2).Interior.Color = vbGreen) And (c.Offset(3, -3).Interior.Color = vbRed Or c.Offset(3, -3).Interior.Color = vbGreen) And (c.Offset(4, -4).Interior.Color = vbRed Or c.Offset(4, -4).Interior.Color = vbGreen) Then
c.Interior.Color = RGB(0, 255, 0)
c.Offset(1, -1).Interior.Color = RGB(0, 255, 0)
c.Offset(2, -2).Interior.Color = RGB(0, 255, 0)
c.Offset(3, -3).Interior.Color = RGB(0, 255, 0)
c.Offset(4, -4).Interior.Color = RGB(0, 255, 0)
End If


Next
 
Upvote 0
I've altered the original to look for 5 instead of 3., and to color them instead of displaying a msgbox. The original one you sent me works perfectly fine for going horizontally or vertically or for diagonally in this direction \ but when I reverse it by adding negative values in offset, to go diagonally this way / it goes crazy with filing in cells when the macro is ran, even if there is not 5 in a row diagonally. The problem only happens when using negative values. Works great otherwise if I take out that section of code causing the issue.
 
Upvote 0
the problem is somewhere in this code. when this code alone runs, a bunch of cells fill in with green

VBA Code:
Dim c As Range, cad As String

On Error Resume Next
For Each c In Range("c4:s15")

'********** check if diagonal row of 5 cells with either green or red fill exist, if so, fill that 5 in with green ************
If (c.Interior.Color = vbRed Or c.Interior.Color = vbGreen) And (c.Offset(1, -1).Interior.Color = vbRed Or c.Offset(1, -1).Interior.Color = vbGreen) And (c.Offset(2, -2).Interior.Color = vbRed Or c.Offset(2, -2).Interior.Color = vbGreen) And (c.Offset(3, -3).Interior.Color = vbRed Or c.Offset(3, -3).Interior.Color = vbGreen) And (c.Offset(4, -4).Interior.Color = vbRed Or c.Offset(4, -4).Interior.Color = vbGreen) Then
c.Offset(4, -4).Interior.Color = RGB(0, 255, 0)
c.Offset(3, -3).Interior.Color = RGB(0, 255, 0)
c.Offset(2, -2).Interior.Color = RGB(0, 255, 0)
c.Offset(1, -1).Interior.Color = RGB(0, 255, 0)
c.Interior.Color = RGB(0, 255, 0)
End If
Next
End Sub
 
Upvote 0
Apparently VBA has problems with the If of more than 3 conditions. I changed it to 3 and 2.

Try this:

VBA Code:
Sub changeColor2()
  Dim c As Range
  
  On Error Resume Next
  For Each c In Range("C4:S15")
    '********** check if diagonal row of 5 cells with either green or red fill exist, if so, fill that 5 in with green ************
    
    If (c.Interior.Color = vbRed Or c.Interior.Color = vbGreen) And _
       (c.Offset(1, -1).Interior.Color = vbRed Or c.Offset(1, -1).Interior.Color = vbGreen) And _
       (c.Offset(2, -2).Interior.Color = vbRed Or c.Offset(2, -2).Interior.Color = vbGreen) Then
       
      If (c.Offset(3, -3).Interior.Color = vbRed Or c.Offset(3, -3).Interior.Color = vbGreen) And _
         (c.Offset(4, -4).Interior.Color = vbRed Or c.Offset(4, -4).Interior.Color = vbGreen) Then
        If Err.Number = 0 Then
          c.Interior.Color = RGB(0, 255, 0)
          c.Offset(1, -1).Interior.Color = RGB(0, 255, 0)
          c.Offset(2, -2).Interior.Color = RGB(0, 255, 0)
          c.Offset(3, -3).Interior.Color = RGB(0, 255, 0)
          c.Offset(4, -4).Interior.Color = RGB(0, 255, 0)
        End If
      End If
      
    End If
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top