I found and modified the below code to remove all rows that don't contain "NHO_Global" in column F, but it deletes everything (it does leave the header row, though).
What did I do wrong and how can I fix it? I need it to leave just the rows that contain "NHO_Global" in column F (simply filtering won't work for my purposes - they need to be deleted).
Sub KeepOnlyNHOGlobal() Dim ws As Worksheet Dim rng As Range Dim lastRow As Long Set ws = ActiveWorkbook.Sheets("NHO_Global") lastRow = ws.Range("F" & ws.Rows.Count).End(xlUp).Row Set rng = ws.Range("F1:F" & lastRow) 'Filter and delete all but the header row With rng .AutoFilter Field:=1, Criteria1:="<>NHO_GLOBAL" .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete End With 'Turn off the filters ws.AutoFilterMode = False End Sub
UPDATE: it turns out that having filters turned on (even with none applied) was the problem. Turning filters off allowed the code to run as expected.