find specific pattern cells in Excel

anand3dinesh

Board Regular
Joined
Dec 19, 2019
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Dear aAll,

Please see attached image, i need VBA code that identifies Yellow filled and hatched cell and mark it as Y in Flag Column.
Any help please?

VBA Query.JPG
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi.
I Suppose Your Yellow Color is RGB(255,255,0) & Your Hatched is 25%Gray , Then this is code:
VBA Code:
Sub FindYellowHatchedCells()
Dim MyRange As Range
Dim Lastrow As Long
Dim Cell As Range

Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Set MyRange = Range("A2:A" & Lastrow)
For Each Cell In MyRange
 If Cell.Interior.Color = RGB(255, 255, 0) Then
     If Cell.Interior.Pattern = xlPatternGray25 Then
       Cell.Offset(0, 1).Value = "Y"
     Else
       Cell.Offset(0, 1).Value = ""
     End If
    
 End If

Next Cell

End Sub
 
Upvote 0
T
Hi.
I Suppose Your Yellow Color is RGB(255,255,0) & Your Hatched is 25%Gray , Then this is code:
VBA Code:
Sub FindYellowHatchedCells()
Dim MyRange As Range
Dim Lastrow As Long
Dim Cell As Range

Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Set MyRange = Range("A2:A" & Lastrow)
For Each Cell In MyRange
If Cell.Interior.Color = RGB(255, 255, 0) Then
     If Cell.Interior.Pattern = xlPatternGray25 Then
       Cell.Offset(0, 1).Value = "Y"
     Else
       Cell.Offset(0, 1).Value = ""
     End If
   
End If

Next Cell

End Sub

Thanks for you Help. i have a question check below line, there will be no data in this column except color fill and hacth cells. how could .End(xlUp) will work?

Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
 
Upvote 0
Sorry anand3dinesh . I think you have Data in Range. Then Use This. Code continue until found a cell with No Color.
VBA Code:
Sub FindYellowHatchedCells()
Dim MyRange As Range
Dim StartRow As Long
Dim Cell As Range
Dim i As Long

StartRow = Range("H2").Row
For i = StartRow To 65536
If Cells(i, 8).Interior.Color <> RGB(255, 255, 255) Then
 If Cells(i, 8).Interior.Color = RGB(255, 255, 0) Then
     If Cells(i, 8).Interior.Pattern = xlPatternGray25 Then
       Cells(i, 9).Value = "Y"
     Else
       Cells(i, 9).Value = ""
     End If
   Debug.Print Cells(i, 8).Address
 End If
 
Else
 Exit Sub
End If
Next i

End Sub

 
Upvote 0
Sorry anand3dinesh . I think you have Data in Range. Then Use This. Code continue until found a cell with No Color.
VBA Code:
Sub FindYellowHatchedCells()
Dim MyRange As Range
Dim StartRow As Long
Dim Cell As Range
Dim i As Long

StartRow = Range("H2").Row
For i = StartRow To 65536
If Cells(i, 8).Interior.Color <> RGB(255, 255, 255) Then
If Cells(i, 8).Interior.Color = RGB(255, 255, 0) Then
     If Cells(i, 8).Interior.Pattern = xlPatternGray25 Then
       Cells(i, 9).Value = "Y"
     Else
       Cells(i, 9).Value = ""
     End If
   Debug.Print Cells(i, 8).Address
End If

Else
Exit Sub
End If
Next i

End Sub

Thanks Mate, That Worked
 
Upvote 0
I see that you already have a suitable answer and I don't know how big your range to check is likely to be but here is an alternative that should do the job without cycling through & checking row-by-row. This code will also work for all relevant cells in the column even if cells with no colour exist between the coloured cells.

VBA Code:
Sub MarkYellowHatch()
  Dim rngLastYellowHatch As Range

  With Application
    .ScreenUpdating = False
    .FindFormat.Clear
    With .FindFormat.Interior
     .Pattern = xlGray25
     .PatternColorIndex = xlAutomatic
     .Color = vbYellow
    End With
    Set rngLastYellowHatch = Columns("A").Find(What:="", SearchDirection:=xlPrevious, SearchFormat:=True)
    If Not rngLastYellowHatch Is Nothing Then
      With Range("A1", rngLastYellowHatch)
        .AutoFilter Field:=1, Criteria1:=RGB(0, 0, 0), Operator:=xlFilterCellColor
        With ActiveSheet.AutoFilter.Filters(1).Criteria1
          .Pattern = xlGray25
          .Color = vbYellow
        End With
        .Offset(1, 1).Resize(.Rows.Count - 1).SpecialCells(xlVisible).Value = "Y"
      End With
      ActiveSheet.AutoFilterMode = False
    End If
    .FindFormat.Clear
    .ScreenUpdating = True
  End With
End Sub

1607328189290.png
 
Upvote 0
Solution
I see that you already have a suitable answer and I don't know how big your range to check is likely to be but here is an alternative that should do the job without cycling through & checking row-by-row. This code will also work for all relevant cells in the column even if cells with no colour exist between the coloured cells.

VBA Code:
Sub MarkYellowHatch()
  Dim rngLastYellowHatch As Range

  With Application
    .ScreenUpdating = False
    .FindFormat.Clear
    With .FindFormat.Interior
     .Pattern = xlGray25
     .PatternColorIndex = xlAutomatic
     .Color = vbYellow
    End With
    Set rngLastYellowHatch = Columns("A").Find(What:="", SearchDirection:=xlPrevious, SearchFormat:=True)
    If Not rngLastYellowHatch Is Nothing Then
      With Range("A1", rngLastYellowHatch)
        .AutoFilter Field:=1, Criteria1:=RGB(0, 0, 0), Operator:=xlFilterCellColor
        With ActiveSheet.AutoFilter.Filters(1).Criteria1
          .Pattern = xlGray25
          .Color = vbYellow
        End With
        .Offset(1, 1).Resize(.Rows.Count - 1).SpecialCells(xlVisible).Value = "Y"
      End With
      ActiveSheet.AutoFilterMode = False
    End If
    .FindFormat.Clear
    .ScreenUpdating = True
  End With
End Sub

View attachment 27429
Thanks Mate, this works great.
apologies i am repling you late
 
Upvote 0
You're welcome. Follow-up any time is appreciated. (y)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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