OK. I am assuming that column Q is an unused column. If this is not correct then everywhere in the code that references column Q must be edited to the reference of a blank column. Also the code line where the AutoFilter is set "Field:=17" must be edited to reflect the number of the column (Column 17 is column "Q").
Replace all of the previous code with the following and then Run Macro1 (If you use F5 to run the code then the cursor should be somewhere between Sub Macro1() and the End Sub for that macro name. If the cursor is not located within the sub then you will get a pop up dialog with the option to select Macro1 and then click the Run button.)
Sub Macro1()
Dim ws As Worksheet
Dim lngLastRow As Long
Set ws = Worksheets("Sheet1") 'Edit "Sheet1" to your worksheet name
'Find last used row on worksheet (ws.Cells is entire worksheet range)
lngLastRow = LastRowOrCol(True, ws.Cells)
With ws
.AutoFilterMode = False 'Start by turning off AutoFilter (If already on)
.Range("Q1") = "CountZeros"
.Range("Q1").Font.Bold = True
.Cells(2, "Q") = "=COUNTIF(I2:P2,0)" 'Enter formula in cell Q2
'Copy the formula down to last row of data
.Cells(2, "Q").Copy Destination:=.Range(.Cells(2, "Q"), .Cells(lngLastRow, "Q"))
Application.CutCopyMode = False 'Need to cancel copy with latest update of Excel
.Range(.Cells(1, "A"), .Cells(lngLastRow, "Q")).AutoFilter 'Turn on Autofilter
.AutoFilter.Range.AutoFilter Field:=17, Criteria1:="<>8" 'Set filter NOT equal to 8
.Columns("Q:Q").Hidden = True 'Optional to hide column "Q" containing formula
End With
End Sub
Function LastRowOrCol(bolRowOrCol As Boolean, Optional rng As Range) As Long
'Finds the last used row or column in a worksheet
'First parameter is True for Last Row or False for last Column
'Third parameter is optional
'Must be specified if not ActiveSheet
Dim lngRowCol As Long
Dim rngToFind As Range
If rng Is Nothing Then
Set rng = ActiveSheet.Cells
End If
If bolRowOrCol Then
lngRowCol = xlByRows
Else
lngRowCol = xlByColumns
End If
With rng
Set rngToFind = rng.Find(What:="*", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=lngRowCol, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
End With
If Not rngToFind Is Nothing Then
If bolRowOrCol Then
LastRowOrCol = rngToFind.Row
Else
LastRowOrCol = rngToFind.Column
End If
End If
End Function