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>
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Lets try this again:

Try this:
Code:
Sub Auto_Filter()
'Modified 9-3-17 6:55 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
Hi M.A.I.T, I think What Peter and Rick are questioning is the use of A2, as autofilter requires a header row. As you have it row 2 wouldn't be included in the formatting.

The normal way around it is to offset and resize the range i.e.


Code:
Sub Auto_Filter()
'Modified 9-3-17 6:55 AM EDT
Application.ScreenUpdating = False

    With Range("[COLOR="#FF0000"]A1[/COLOR]: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"
        [COLOR="#FF0000"].Offset(1).Resize(.Rows.Count - 1).[/COLOR]SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3
        .AutoFilter
    End With
    
Application.ScreenUpdating = True
End Sub

or if there is never any data below the filter range then just Offset(1) would suffice.
 
Upvote 0
As suggested in thread #13 ?
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
 
Upvote 0
Yes I always have trouble with Filters and keep forgetting about this offset thing. Loops are easier for me but then people complain saying loops are too slow. Thanks for reminding me once again about offset. I think you mentioned this to me before and I forgot. Thanks for your help.
Hi M.A.I.T, I think What Peter and Rick are questioning is the use of A2, as autofilter requires a header row. As you have it row 2 wouldn't be included in the formatting.

The normal way around it is to offset and resize the range i.e.


Code:
Sub Auto_Filter()
'Modified 9-3-17 6:55 AM EDT
Application.ScreenUpdating = False

    With Range("[COLOR=#ff0000]A1[/COLOR]: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"
        [COLOR=#ff0000].Offset(1).Resize(.Rows.Count - 1).[/COLOR]SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3
        .AutoFilter
    End With
    
Application.ScreenUpdating = True
End Sub

or if there is never any data below the filter range then just Offset(1) would suffice.
 
Upvote 0
Forgive me I have one extra line of uneeded code.
For those who might find this interesting, that one line of unneeded code cost one-half second extra in the execution speed for the 300,000 record set the OP said he had... on my computer, 7.96 seconds with it as opposed to 7.45 seconds without it.
 
Upvote 0
@Rick Rothstein would you mind putting run time for Code in #13 ? I thought for 300k rows, it would be faster to loop over an array; which is what M.A.I.T commented they usually code with but some find loops slower than filters?
 
Last edited:
Upvote 0
which is what M.A.I.T commented they usually code with but some find loops slower than filters?

I think you will find M.A.I.T. is referring to looping over a range not an array.
 
Upvote 0
@Rick Rothstein would you mind putting run time for Code in #13 ?
?
I get a "Type Mismatch" error on this line of code (when x = 1, so the loop never runs)...
Code:
[table="width: 500"]
[tr]
	[td]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[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,841
Members
449,193
Latest member
MikeVol

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