VBA Autofilter not equal to
Asked Answered
B

1

8
Sub Macro1()
‘Remove all except validated
ActiveSheet.Range("$A$1:$H$5202").AutoFilter field:=8, Criteria1:<>"Validated"

Activesheet.Range("$A$2:$O$99999").SpecialCells(xlCellTypeVisible).Select
Selection.EntireRow.Delete

ActiveSheet.ShowAllData
 End sub

How to replace "not equal than" in VBA? <> does not work.

Bedchamber answered 22/11, 2018 at 8:48 Comment(0)
O
20

If you want your filter criteria to exclude "Validated", then try changing this line:

ActiveSheet.Range("$A$1:$H$5202").AutoFilter field:=8, Criteria1:<>"Validated"

to

ActiveSheet.Range("$A$1:$H$5202").AutoFilter field:=8, Criteria1:="<>Validated"

Note that the = in Criteria:= doesn't have anything to do with your filter criteria. (It relates to VBA and how you provide an argument to a named parameter.)

Obituary answered 22/11, 2018 at 10:59 Comment(4)
I see. Thanks for your help.Bedchamber
How would it be applied in case the criteria itself is a variable?Chamomile
@AntonioSantos, depending on the variable type, you might be able to do something like: ActiveSheet.Range("$A$1:$H$5202").AutoFilter Field:=8, Criteria1:="<>" & someVariable. This assumes that there is a variable called someVariable. If you need to filter by date, you might need to use CDbl(someVariable), since filtering dates programmatically can be a little awkward if I understand correctly.Obituary
Worked nicely! Thank you!Chamomile

© 2022 - 2024 — McMap. All rights reserved.