Probably don't need a macro.
- Add an additional column header in column M. Header suggestion CountZeros
- In cell M2 enter the formula =COUNTIF(E2:L2,0)
- Copy the formula down
- Apply AutoFilter to the range of data
- Select Dropdown on column M
- Select Number filters (Above the list of options displayed)
- Select Does not equal
- Enter 8 (8 is the max number of zeros for the 8 columns)
- Click OK
However, if you want to do the above with VBA code then the following.
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("M1") = "CountZeros"
.Range("M1").Font.Bold = True
.Cells(2, "M") = "=COUNTIF(E2:L2,0)" 'Enter formula in cell M2
'Copy the formula down to last row of data
.Cells(2, "M").Copy Destination:=.Range(.Cells(2, "M"), .Cells(lngLastRow, "M"))
Application.CutCopyMode = False 'Need to cancel copy with latest update of Excel
.Range(.Cells(1, "A"), .Cells(lngLastRow, "M")).AutoFilter 'Turn on Autofilter
.AutoFilter.Range.AutoFilter Field:=13, Criteria1:="<>8" 'Set filter not equal to 8
.Columns("M:M").Hidden = True 'Optional to hide column "M" with formula (Line added after initial post)
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