Excel VBA Criteria1 Not Equal: Filter Data Excluding Values
In this article
Use Excel VBA with Criteria1 not equal to filter out specific values from a range, leaving only rows that match your exclusion rules. This is essential for cleaning datasets, preparing reports, or isolating exceptions without manually deleting rows. You need Excel with VBA enabled and a data range with headers. The technique works with AutoFilter and AdvancedFilter methods, and the key is using the <> operator correctly in string form.
What You'll Need
- Microsoft Excel 2010 or later (VBA supported)
- A worksheet with structured data and column headers
- Enable Developer tab if not visible: File > Options > Customize Ribbon > check Developer
- Basic understanding of VBA syntax and ranges
Step-by-Step
- Open your Excel workbook and press
Alt + F11to open the VBA Editor. - In the Project Explorer, right-click your workbook name (e.g., VBAProject (YourWorkbook.xlsx)) and choose Insert > Module.
- Copy and paste this example code into the module:
Sub FilterNotEqual()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change to your sheet name
Dim rng As Range
Set rng = ws.Range("A1:D100") 'Adjust range to match your data
'Clear any existing filters
rng.AutoFilter
'Apply filter: exclude rows where column B equals "Rejected"
rng.AutoFilter Field:=2, Criteria1:="<>Rejected"
End Sub
- Modify the worksheet name in
Set ws = ThisWorkbook.Worksheets("Sheet1")to match your sheet’s actual name (case-sensitive). - Adjust the range
"A1:D100"to cover all your data, including headers. Ensure it starts at the header row. - Change
Field:=2to the column number you want to filter. Column A is 1, B is 2, etc. - Replace
"Rejected"with the exact text or value you want to exclude. Use quotes around text; numbers don’t need quotes. - Press
F5to run the macro. The worksheet will now hide rows where column B equals "Rejected". - To filter multiple exclusions, use an array:
Criteria1:=Array("<>Rejected", "<>Cancelled"), Operator:=xlFilterValues.
Common Mistakes
- Forgetting the ampersand and angle brackets (
<>) — writing"<>Rejected"without quotes around the operator breaks the syntax. - Using
Criteria1:="<>" & cellValuewhencellValuecontains a string — this concatenates the operator correctly but only works if the cell has a text value. If the cell is empty or numeric, use direct string comparison. - Referencing a non-existent column number (e.g., Field:=5 when you only have 3 columns) causes a runtime error.
- Not clearing previous filters before applying a new one, leading to overlapping or conflicting filters.
- Using single quotes instead of double quotes around the criteria string — VBA requires double quotes.
- Filtering on a column that contains merged cells — AutoFilter fails on merged ranges. Unmerge cells first.
Tips
- Always test your filter on a copy of your data before applying to live reports.
- Use
rng.AutoFilter Field:=x, Criteria1:="<>"to exclude blank cells — this hides empty rows in that column. - To filter multiple values to exclude, use an array with
Operator:=xlFilterValuesand list each exclusion as"<>Value"inside the array. - Use
Debug.Print rng.AutoFilter.Criteria1in the Immediate Window (Ctrl + Gin VBA Editor) to verify the criteria was parsed correctly. - For numeric exclusion, use
Criteria1:="<>100"— no quotes needed around numbers, but the operator must still be in quotes. - Combine with
On Error Resume Nextif you’re unsure whether the sheet or range exists, but always follow with error handling to avoid silent failures. - Use
rng.AutoFilter Mode:=xlFilterShowAllto reset filters programmatically before applying new ones.
Troubleshooting
- If the filter doesn’t apply, check that the range includes headers. AutoFilter requires them.
- If you get Run-time error '1004': Application-defined or object-defined error, verify the field number matches the column position in your range. If your range starts at column C, then Field:=1 refers to column C, not column A.
- If no rows are hidden despite correct syntax, check for leading/trailing spaces in the criteria text. Use
Trim(" Rejected ")if needed, or use wildcards:"*<>Rejected*"is invalid — stick to exact matches. - If the macro runs but nothing changes, ensure the worksheet isn’t protected. Go to Review > Unprotect Sheet.
- If Excel crashes or freezes, reduce the range size. Filtering hundreds of thousands of rows can overload memory — use
ws.UsedRangeinstead of hard-coded ranges if data size varies. - For dates, use
Criteria1:="<>" & DateValue("2024-01-01")— convert dates to serial numbers usingCDbl()if necessary. - Check for hidden characters: copy the value you want to exclude into Notepad, then paste back into VBA to ensure no invisible formatting is present.
Frequently Asked Questions
Can I use Criteria1 not equal with wildcards?
No. Wildcards like * or ? are ignored when used with the <> operator. UseCriteria1:="<>*text*" will not work. Instead, use AdvancedFilter with custom criteria ranges for pattern-based exclusion.
Does this work with numbers and dates?
Yes. For numbers:Criteria1:="<>50". For dates: Criteria1:="<>" & DateSerial(2024,1,1) or Criteria1:="<>45292" (the serial number for Jan 1, 2024).
Why does my filter show all rows even when the criteria seems right?
Most likely, the criteria text doesn’t match exactly. Check for case sensitivity (Excel AutoFilter is not case-sensitive, but VBA string comparison might be if used elsewhere), extra spaces, or inconsistent formatting (e.g., "N/A" vs. "N/A " with a trailing space).Can I exclude multiple values at once?
Yes. Use an array:Criteria1:=Array("<>Apple", "<>Orange"), Operator:=xlFilterValues to exclude both. You can include up to 1000 values in the array.
What if I want to exclude blank cells too?
UseCriteria1:="<>" — this excludes empty cells. You can combine it with other exclusions using an array: Array("<>Rejected", "<>"). Note: this will hide rows where the field is empty or contains "Rejected".
Can I use this with AdvancedFilter instead of AutoFilter?
Yes, but the syntax differs. AdvancedFilter requires a separate criteria range on the worksheet with the condition written as≠ or <>Value in a cell. AutoFilter is simpler for VBA automation.