PDA

View Full Version : Delete Entire row if cell value does not have either two of specific word



analyst
02-25-2014, 05:47 PM
Data range starts from A9:M9, and last row varies on day to day basis.

Adjacent cells like A1:M8, have data, which should not get disturbed.

Column B has identifier flag, which in case is not there, entire row should be removed.

So, Macro should
1) search from A9:M9 till last available row (Range), and
2) see, if row cell in Column B has either of the word 'XX' or 'YY', then it should leave entire row as it is and (row be retained)
3) Else, If row Cell in Column B has word, other than 'XX' OR 'YY' macro should delete entire row is moved (shift) up, (ensure Range A1:M8) remains untouched.

How to do this?

Thanks

Admin
02-25-2014, 10:55 PM
Try


Option Explicit

Sub kTest()

Dim r As Range

With Range("A9:m" & Range("a" & Rows.Count).End(xlUp).Row)
.AutoFilter Field:=2, Criteria1:="<>T", Operator:=xlAnd, Criteria2:="<>v"
On Error Resume Next
Set r = .Cells(1).Offset(1).Resize(.Rows.Count - 1, 1).SpecialCells(12)'remove highlighted, if row 9 is not header
If Not r Is Nothing Then
r.EntireRow.Delete
End If
.AutoFilter
End With

End Sub