How to use <> array criteria - excel

let me ask my problem, i'm just a beginner in vba.
I have to filter a column with criteria
<>array ("0402", "0603", "0805", "1206").
i have tried this one but not work for me :
Rows (1).AutoFilter Field:=9, Criteria1:=Array("=<>0402", =<>0603", "=<>0805", "=<>1206"), Operator:=xlFilterValues
For information, value in column field 9 is paste value of right text function. So what i have to define the character? is it text or numeric? and how about the array?

you can use only two not equal to criteria's. Refer the below post for more clarity
Autofilter for multiple not equal values

Public Sub REName_()
Dim d1() As Variant: d1 = Array("0402", "0603", "0805", "1206")
Dim r As Range: Set r = Cells(1, 1).CurrentRegion
r.AutoFilter Field:=1, Criteria1:=d1, Operator:=xlFilterValues
End Sub

Related

Filtering based on a range of names from a different sheet

I'm trying to filter on range of names, without adding them manually, not only one name like right now, from the Pos_data to T_Data
My code:
Dim category As Range
With Worksheets("Pos_Data")
Set category = .Range("U2")
End With
With Worksheets("T_Data")
With .Range("A1:CP1503")
.AutoFilter Field:=14, Criteria1:=category, VisibleDropDown:=True
End With
End With
End Sub
Names appear in the column U of Pos_Data
Question is: how to have a filter that picks all the names from column U and then applies it in T_Data Column N
Like here, I've got names and without manually writing them in the code I would like to filter on them in a new tab column N
You must construct an array of your names from the values in Column U. This example will hopefully give you a start.
Option Explicit
Sub FilterForNames()
Dim categories As Range
Dim categoryList As Variant
'--- build an array of the names in the filter
With Worksheets("Pos_Data")
Set categories = .Range("U2:U5")
ReDim categoryList(1 To categories.Cells.Count)
Dim i As Long
For i = 1 To UBound(categoryList)
categoryList(i) = categories.Cells(i, 1).Value
Next i
End With
With Worksheets("T_Data")
With .Range("A1:CP1503")
.AutoFilter Field:=14, Operator:=xlFilterValues, Criteria1:=categoryList
End With
End With
End Sub

How to Convert Numeric Value to String in my code?

The code below makes a copy of the master sheet for each of the cells in the list (Named "Splitcode"), it then filters the first column in the data (Named "MasterData") and deletes any rows that don't have that cell in them. and loops until a sheet is created for every cell.
The code works perfectly on text values on column1 and on the list.
but it won't work on numeric values (e.g account numbers).
I have been told I should be adding a CStr() Function, but I've never used it before so I don't know where to exactly add it.
Sub SplitandFilterSheet()
Dim Splitcode As Range
Sheets("Master").Select
Set Splitcode = Range("Splitcode")
For Each Cell In Splitcode
Sheets("Master").Copy After:=Worksheets(Sheets.Count)
ActiveSheet.Name = Cell.Value
With ActiveWorkbook.Sheets(Cell.Value).Range("MasterData")
.AutoFilter Field:=1, Criteria1:="<>" & Cell.Value,Operator:=xlFilterValues
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
ActiveSheet.AutoFilter.ShowAllData
Next Cell
End Sub
Consider this: CStr( expression )
Dim LValue As String
LValue = CStr(8)
The LValue variable would now contain the string value of "8"
I see that you have Cell.Value in 3 places. Also, check out this link.
https://www.excel-easy.com/examples/number-text-filters.html
Finally, you may want to simplify things a bit. If you have a header row in your range that you don't want to delete, add an offset to the range to exclude it:
ActiveSheet.Range("$A$1:$I$" & lines).Offset(1, 0).SpecialCells _
(xlCellTypeVisible).EntireRow.Delete

Filtering By blanks in VBA

Could anyone give me some insight on how to Filter/Delete Blanks using VBA code? For some reason when I record a Macro to do this it is not allowing some of my custom functions built using VBA to hold their values. Thanks.
The below code will delete out rows that have a blank in a selected column. The code below assumes the second column in your data is being tested for blanks. Let us know if you need additional assistance.
Sub DeleteBlanks()
Dim rDataToProcess As Range
Set rDataToProcess = Sheet1.Range("A1").CurrentRegion
'Field in the below method refers to the column that is being filtered, so the second colum
rDataToProcess.AutoFilter field:=2, Criteria1:=""
rDataToProcess.Offset(1).Resize(rDataToProcess.Rows.Count).EntireRow.Delete
Sheet1.AutoFilterMode = False
End Sub
An alternative to delete cells that are blank is to set a range, and use Range([your range]).SpecialCells(xlCellTypeBlanks).Delete
edit: If you want to delete the entire row, Range([your range]).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Just as #user3561813 said and please take a look at this link for more complicated filters such as multiple criteria, etc:
ActiveSheet.Range("AD1").AutoFilter Field:=30, Criteria1:="", Operator:=xlOr, Criteria2:="XXXX"
For example, the above code filters for both Blanks and "XXXX" fields
Sub Blank_Cells_Filter()
'Apply filters to include or exclude blank cells
Dim lo As ListObject
Dim iCol As Long
'Set reference to the first Table on the sheet
Set lo = Sheet1.ListObjects(1)
'Set filter field
iCol = lo.ListColumns("Product").Index
'Blank cells – set equal to nothing
lo.Range.AutoFilter Field:=iCol, Criteria1:="="
'Non-blank cells – use NOT operator <>
lo.Range.AutoFilter Field:=iCol, Criteria1:="<>"
End Sub

AutoFilter - Dynamically change in Filter Criteria

I'm having the same kind of question as in this link -
Get AutoFilter sort criteria and apply on second sheet
I've gone thru the link but not able to get the required output.
I've the filtered criteria in Sheet1 (which we can change as required) on one of the column values (eg: col 10) and now based on what ever the data in column 10 which are shown based on the filter criteria, I want to filter on sheet2 with the data in sheet 1.
I have seen that many of them using with static values in ARRAY as shown but how can I autofilter dynamically changing values in the sheet1 and filtering in Sheet2. Please advise
.AutoFilter Field:=10, Criteria1:=Array("value1", "value2"), Operator:=xlFilterValues
What if you just define the array in VBA?
Dim CritArray(2) as String
CritArray(0) = Cells(1,1).Value
CritArray(1) = Cells(2,1).Value
Then just editing your line of code:
.AutoFilter Field:=10, Criteria1:=Array(CritArray(0),CritArray(1)), Operator:=xlFilterValues
I dont know how many criteria you have (or their location), but you can add/edit as such. I based this off the fact you only have 2 criteria, but it can be enlarged of course.
I think you want something like this:
Sub tgr()
Dim wsData As Worksheet
Dim wsCriteria As Worksheet
Dim arrCriteria As Variant
Set wsData = Sheets("Sheet2")
Set wsCriteria = Sheets("Sheet1")
arrCriteria = Application.Transpose(wsCriteria.Range("J4", wsCriteria.Range("J4").End(xlDown)).Value)
With wsData.UsedRange
.AutoFilter 10, arrCriteria, xlFilterValues
End With
Set wsData = Nothing
Set wsCriteria = Nothing
If IsArray(arrCriteria) Then Erase arrCriteria
End Sub

How to remove blank rows when using Excel VBA Autofilter

I have written a macro that searches through workbook and applies an autofilter to any listobjects which have a column named "Code". However, when I apply the filter, it does not filter out the blank rows. Any idea on how I can filter these out?
Here is the code which applies the filter:
Public Sub ApplyFilter(filter As Variant)
Dim wb As Workbook
Dim ws As Worksheet
Dim lo As ListObject
Set wb = ActiveWorkbook
' Loop through each sheet in the workbook
For Each ws In wb.Sheets
' Find any listobjects within the sheet
For Each lo In ws.ListObjects
Dim r As Integer
' Find the column named Code and filter on this column
r = lo.Range.Rows(1).Find("Code").Column
' Clear any existing filter
lo.Range.AutoFilter Field:=r
' If the filter code is not "All Categories", 999, apply the filter
If filter(0) <> 999 Then
lo.Range.AutoFilter Field:=r, Criteria1:=filter, Operator:=xlFilterValues
End If
Next
Next
End Sub
The filter that is passed in is an array which may just have one criteria, or many. I have also tried adding criteria2:="", but that did not change anything.
Let me know if you have any ideas. Thanks!
Here is the other related code:
Public Sub FilterInvoice(filter As Range)
Me.ApplyFilter Me.BuildFilter(filter)
End Sub
Public Function BuildFilter(filter As Range) As Variant
Dim r As Range
Dim arFilter() As String
' Get the cell of the current category
Set r = Range("Categories").Find(filter.Value)
' Set the initial filter value to the category id
ReDim Preserve arFilter(1)
arFilter(0) = r.Offset(0, -1).Value
' Find any child categories, add child id's to filter array
For c = 1 To Application.CountIf(Range("Categories").Columns(3), arFilter(0))
Dim PrevChild As Range
' Expand the filter array
ReDim Preserve arFilter(c + 1)
If c = 1 Then
Set PrevChild = Range("Categories").Columns(3).Find(arFilter(0))
Else
' If it is not the first time through the loop, look for the next child after PrevChild
Set PrevChild = Range("Categories").Columns(3).Find(arFilter(0), PrevChild)
End If
' Offset the found child to get its code, add it to the filter array
arFilter(c) = PrevChild.Offset(, -2)
Next
' Add "<>" and "<900" to the criteria list to hide blank rows
'ReDim Preserve arFilter(UBound(arFilter) + 2)
'arFilter(UBound(arFilter) - 1) = "<>"
'arFilter(UBound(arFilter)) = "<900"
'Return the filter array
BuildFilter = arFilter
End Function
If you are filtering by multiple criteria using an array then by not including "=" the autofilter should filter the blanks. For example this will NOT filter blanks:
Criteria1:=Array("test", "2", "3", "4", "=")
Failing that you may need to hide them manually using specialcells(xlcelltypeblanks).
EDIT:
Okay I think I might have confused you there with my first solution. I have removed it.
Now that I can see your code I think what might be happening is that as you are looping through the range and adding your criteria you are probably adding a blank cell. Step through the loop one at a time and make sure this is not the case. You could add this to display the filter and make sure it does not contain blanks:
Debug.Print Join(arfilter, ",")
I know this is an old question but I coudln't find any satisfying answer anywhere on the Internet
So I'd like to share a solution which seems to work pretty well:
ActiveSheet.Range("$A$1:$V$3000").AutoFilter Field:=ActiveSheet.Range("$A$1:$V$1").Find("Monitoring").Column, _
Criteria1:="<>Done", Operator:=xlFilterValues
blank cells are still present so we need no filter them out
ActiveSheet.Range("$A$1:$V$1").Find("Monitoring").EntireColumn.SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
Of course because it uses xlFilterValues you can use an Array filter as well
ActiveSheet.Range("$A$1:$V$3000").AutoFilter Field:=ActiveSheet.Range("$A$1:$V$1").Find("Monitoring").Column, _
Criteria1:=Array( _
"Department1", "Department2", "Department3", "Department4", _
"Department5", "Department6", "="), Operator:=xlFilterValues
Hope you enjoy!

Resources