Need VBA to Highlight Rows with all N's

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good morning,

I have over 300,000 rows of data in Columns A-F. The rows have either N or Y in them. What I need to do is to highlight all rows that have just N's in them. I do not want a formula and Code will help! Thanks in advance.
<style type="text/css">
table.tableizer-table {
font-size: 12px;
border: 1px solid #CCC ;
font-family: Arial, Helvetica, sans-serif;
}
.tableizer-table td {
padding: 4px;
margin: 3px;
border: 1px solid #CCC ;
}
.tableizer-table th {
background-color: #104E8B ;
color: #FFF ;
font-weight: bold;
}
</style>
<table class="tableizer-table">
<thead><tr class="tableizer-firstrow"><th>N</th><th>N</th><th>N</th><th>N</th><th>N</th><th>N</th></tr></thead><tbody>
<tr><td>Y</td><td>N</td><td>N</td><td>N</td><td>N</td><td>Y</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>Y</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td></tr>
<tr><td>Y</td><td>N</td><td>N</td><td>N</td><td>N</td><td>Y</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>Y</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>Y</td><td>N</td><td>N</td><td>N</td><td>N</td><td>Y</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td></tr>
</tbody></table>
 
Thanks JackDanIce and My Aswer Is This! I appreciate your guidance. What I meant and failed to convey myself is if the row had either NNNNNN or YNNNNN. Thanks Again!
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this:
Code:
Sub Auto_Filter()
'Modified 9-2-17 7:45 PM EDT
Application.ScreenUpdating = False

    With Range("A2:F" & Cells(Rows.Count, "A").End(xlUp).Row)
        .AutoFilter Field:=1, Criteria1:="Y", Operator:=xlOr, Criteria2:="N"
        .AutoFilter Field:=2, Criteria1:="N"
        .AutoFilter Field:=3, Criteria1:="N"
        .AutoFilter Field:=4, Criteria1:="N"
        .AutoFilter Field:=5, Criteria1:="N"
        .AutoFilter Field:=6, Criteria1:="N"
        .SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3
        .AutoFilter
    End With
    
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
Code:
Sub Auto_Filter()
    
    Dim LR              As Long
    Dim x               As Long
    Dim i               As Long
    Dim arr()           As Variant
    Dim arrPattern(1 To 2)    As Variant
    Const delim         As String = "|"
    
    arrPattern(1) = "NNNNNN"
    arrPattern(2) = "YNNNNN"
    
    Application.ScreenUpdating = False
        
    With ActiveSheet
        If .AutoFilterMode Then .AutoFilterMode = False
        .Cells.Interior.ColorIndex = xlNone
        LR = .Cells(.Rows.Count, 1).End(xlUp).row
        arr = .Cells(1, 1).Resize(LR, 7).Value
        For x = LBound(arr, 1) To UBound(arr, 1)
            If InStr(arrPattern(1) & delim & arrPattern(2), Join(Application.Index(arr, x, 0), "")) * Len(Join(Application.Index(arr, x, 0), "")) > 0 Then arr(x, UBound(arr, 2)) = True
        Next x
        
        With .Cells(1, 1).Resize(UBound(arr, 1), UBound(arr, 2))
            .Value = arr
            .AutoFilter Field:=UBound(arr, 2), Criteria1:=True
            .Offset(1).Resize(UBound(arr, 1) - 1, UBound(arr, 2) - 1).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 4
        End With

        .AutoFilterMode = False
        .Cells(1, UBound(arr, 2)).Resize(UBound(arr, 1)).ClearContents
    End With
    
    Application.ScreenUpdating = True
    Erase arr
    Erase arrPattern
       
End Sub
 
Last edited:
Upvote 0
Try this:
Code:
Sub Auto_Filter()
'Modified 9-2-17 7:45 PM EDT
Application.ScreenUpdating = False

    With Range("A2:F" & Cells(Rows.Count, "A").End(xlUp).Row)
        [B][COLOR="#FF0000"].AutoFilter Field:=1, Criteria1:="Y", Operator:=xlOr, Criteria2:="N"[/COLOR][/B]
        .AutoFilter Field:=2, Criteria1:="N"
        .AutoFilter Field:=3, Criteria1:="N"
        .AutoFilter Field:=4, Criteria1:="N"
        .AutoFilter Field:=5, Criteria1:="N"
        .AutoFilter Field:=6, Criteria1:="N"
        .SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3
        .AutoFilter
    End With
    
Application.ScreenUpdating = True
End Sub
Since the only possible values in the (filled) cells are Y or N, there is no need to set the filter for Field 1 because the OP is looking for either a Y or N there... every filled line automatically matches that criteria... so the red highlighted line can be deleted.
 
Last edited:
Upvote 0
And how do you come to this conclusion:
"Since the only possible values in the (filled) cells are Y or N,"

I have never seen him state the only values will be Y or N




Since the only possible values in the (filled) cells are Y or N, there is no need to set the filter for Field 1 because the OP is looking for either a Y or N there... every filled line automatically matches that criteria... so the red highlighted line can be deleted.
 
Upvote 0
And how do you come to this conclusion:
"Since the only possible values in the (filled) cells are Y or N,"

I have never seen him state the only values will be Y or N
From the original message... "The rows have either N or Y in them."
 
Last edited:
Upvote 0
OK here is a modified script to correct one unneeded line of my script:
This is assuming there will never be anything in Column "A" but a "N" or a "Y"

Code:
Sub Auto_Filter()
'Modified 9-3-17 12:36 AM EDT
Application.ScreenUpdating = False
    With Range("A2:F" & Cells(Rows.Count, "A").End(xlUp).Row)
        .AutoFilter Field:=2, Criteria1:="N"
        .AutoFilter Field:=3, Criteria1:="N"
        .AutoFilter Field:=4, Criteria1:="N"
        .AutoFilter Field:=5, Criteria1:="N"
        .AutoFilter Field:=6, Criteria1:="N"
        .SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3
        .AutoFilter
    End With
    
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Unless I have missed it, there doesn't seem to be any mention of the existence or not of a header row, but given the OP's acceptance of the earlier code, like other responders I'm assuming there is a header row in row 1.
If that is so, the slight readjustment of this 2 to a 1 (as it was in the original code) would be required, otherwise row 2 will be coloured whether or not it meets the criteria.

Code:
    With Range("A[COLOR="#FF0000"][B]2[/B][/COLOR]:F" & Cells(Rows.Count, "A").End(xlUp).Row)
 
Last edited:
Upvote 0
Unless I have missed it, there doesn't seem to be any mention of the existence or not of a header row, but given the OP's acceptance of the earlier code, like other responders I'm assuming there is a header row in row 1.
If that is so, the slight readjustment of this 2 to a 1 (as it was in the original code) would be required, otherwise row 2 will be coloured whether or not it meets the criteria.
Code:
    With Range("A2:F" & Cells(Rows.Count, "A").End(xlUp).Row)
Which, interestingly, is what he had originally in Message #3 before he modified it in Message #12 to account for the OP's Y or N comment about the first field.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,824
Messages
6,121,783
Members
449,049
Latest member
greyangel23

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