I am using the following code to apply an autofilter to a range and filter on one of the columns by looking for the same value that is contained in several reference cells:
Sub filter()
Range("B6:N9000").AutoFilter Field:=2, Criteria1:=Array(Range("C2").Value, Range("D2").Value, Range("E2").Value )
End Sub
The problem however is the filter only applies the LAST cell referenced in the code, ie for above it ONLY looks up "E2", not "C2" & "D2" & "E2"
Any suggestions? Thanks
To put an answer under this: You want to add the argument Operator:=xlFilterValues to your call, so it will look like this:
Range("B6:N9000").AutoFilter Field:=2, Criteria1:=Array(Range("C2").Value, _
Range("D2").Value, Range("E2").Value), Operator:=xlFilterValues
or
[B6:N9000].AutoFilter Field:=2, _
Criteria1:=Array([C2].Value, [D2].Value, [E2].Value), Operator:=xlFilterValues
Related
I expected the code below to work because it came from a well-rated answer.
If this is a formatting error I would like to correct the error rather than completely changing the approach of the solution. If this way of doing things will not work at all then I am open to trying different approaches.
Any insights would be much appreciated!
ActiveSheet.Range("$A:$AI").AutoFilter Field:=11, Criteria1:=Array("Namib", "Kitchen", _
"Constantia", "Painters", _
"CUSTOM", "Classic", _
"Bench"), _
Operator:=xlFilterValues
You can only filter 2 criteria with wildcards:
Range("$A:$AI").AutoFilter Field:=11, Criteria1:="=*Table*", Operator:=xlOr, Criteria2:="=*Chair*"
Will filter all data containing the word Table or Chair. But this does not work for more than 2 criteria.
'THIS DOES NOT WORK:
Range("$A:$AI").AutoFilter Field:=11, Criteria1:=Array("*Namib*", "*Kitchen*", _
"*Constantia*", "*Painters*", _
"*CUSTOM*", "*Classic*", _
"*Bench*"), _
Operator:=xlFilterValues
See: Set Auto Filtering multiple wildcards for a workaround.
Here's another workaround:
Sub FilterMultipleWildcards()
Dim myCriteria As Variant, criterium As Variant
Dim filteredRng As Range
myCriteria = Array("Namib", "Kitchen", "Constantia", "Painters", "CUSTOM", "Classic", "Bench")
With ActiveSheet
With Intersect(.UsedRange, .Range("$A:$AI"))
Set filteredRng = .Offset(, .Columns.Count).Resize(1, 1)
For Each criterium In myCriteria
.AutoFilter Field:=11, Criteria1:="=*" & criterium & "*"
If Application.WorksheetFunction.Subtotal(103, .Resize(1)) > 1 Then Set filteredRng = Union(filteredRng, .Resize(.Rows.Count - 1, 1).Offset(1, 10).SpecialCells(xlCellTypeVisible))
Next
Set filteredRng = Intersect(filteredRng, .Cells)
.Parent.AutoFilterMode = False
.Resize(.Rows.Count - 1).Offset(1).EntireRow.Hidden = True
End With
filteredRng.EntireRow.Hidden = False
End With
End Sub
whose limitation resides in the "capacity" of the range resulting from Union() method
I have code for autofilter, which always choose "TOP" and "TOP 100" in column R:
Sub Filter()
With Worksheets("Overview")
Range("A1:S1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$S$9999").AutoFilter Field:=18, Criteria1:="=TOP", _
Operator:=xlOr, Criteria2:="=TOP 100"
End With
End Sub
But I would need to add another filter, which will always hide zeros in column P. I mean, that I know, that I have to put values into "Criteria", which I would like to filtered. But those values will be variables, but there always will be 0, which I would need to hide. Does anyone know, how to do that, please?
Many thanks!
Please try Criteria1:="<>0" for filtering all except 0
This code will also filter out 0 from column P:
Sub Filter()
With Worksheets("Overview").Range("$A$1:$S$9999")
.AutoFilter Field:=18, Criteria1:="=TOP", Operator:=xlOr, Criteria2:="=TOP 100"
.AutoFilter Field:=16, Criteria1:="<>0"
End With
End Sub
Try:
Sub Filtre()
Dim r As Range
Set r = Worksheets("Overview").Range("A:S")
r.AutoFilter Field:=18, Criteria1:="=TOP", Operator:=xlOr, Criteria2:="=TOP 100"
r.AutoFilter Field:=16, Criteria1:="<>0", Operator:=xlAnd
End Sub
I use this code to apply a length formula and then Autofill till the last visible cell but getting an error
Runtime Error '1004'- Method 'Range' of object_Global' failed
Code
Range("C2").Select
ActiveCell.FormulaR1C1 = "=LEN(RC[-1])"
Selection.AutoFill Destination:=Range("C2:C" & Lastrow).SpecialCells(xlCellTypeVisible).Select
Selection.Copy
from your code it seems you want the length of cells in COl B. The below code works for me.
Sub x()
Range("C2:C" & Range("B" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=LEN(RC[-1])"
End Sub
As always it's better to stay away from Select, ActiveCell and Selection.
Try the code below:
Dim FitRng As Range, Lastrow As Long
Range("C2").FormulaR1C1 = "=LEN(RC[-1])"
Set FitRng = Range("C2:C" & Lastrow).SpecialCells(xlCellTypeVisible)
FitRng.FillDown
If you don't want to use the FillDown method, you can simply use:
FitRng.FormulaR1C1 = "=LEN(RC[-1])"
Filter field has many numbers, formula (result is "-") and blank.
How to write a VBA code to filter all numbers and exclude "-" and blank.
"-" is not text or string it's the result of a formula.
On Error Resume Next
ActiveSheet.ShowAllData
Range("G8").AutoFilter Filed:=7, Criteria2:="="
Range("N8").AutoFilter Field:=14, Criteria1:="<>-", _
Operator:=xlAnd, Criteria2:="<>"
Try this:
Edit1: For your example, it should be:
Range("N8").AutoFilter Field:=1, Criteria1:="<>-" _
, Criteria2:="<>", Operator:=xlAnd
This will filter out blanks and cells with - as a result of formula.
Take note that you're only working one Cell N8 which only have 1 field of data.
Edit2: Another way to make it work is to explicitly define the range you're working on.
Dim r As Range
Set r = Sheets("Sheet1").Range("A1:N100") 'change to suit
r.AutoFilter Field:=14, Criteria1:="<>-" _
, Criteria2:="<>", Operator:=xlAnd
Is this what you're trying? HTH.
I have the following code:
ActiveSheet.Range("$A$1:$P$201").AutoFilter Field:=5, Criteria1:="=10"*
When I click on the filter and type in the search bar 10* I get all results that start with 10. When using the macro, that doesn't work. The goal is for the macro to filter using the first two numbers provided by me.
Can you assist?
The core of the problem seems to have been trying to apply a text filter to a numeric field.
Instead of:
ActiveSheet.Range("$A$1:$P$201").AutoFilter Field:=5 ActiveSheet.Range("$A$1:$P$201").AutoFilter Field:=5, Criteria1:="=10"*
just:
ActiveSheet.Range("$A$1:$P$201").AutoFilter Field:=5, _
Criteria1:=">=10000", Operator:=xlAnd, Criteria2:="<=10999"
seems to have worked.
The following will work only if the values are text:
Sub Macro2()
ActiveSheet.Range("$A$1:$P$201").AutoFilter Field:=5, Criteria1:="=10*", _
Operator:=xlAnd
End Sub
If the values are not text, then use a "helper" column.
EDIT#1:
For postal codes in column E, this will filter out (hide) rows not containing "10*" codes:
Sub GoingPostal()
Dim r As Range
For Each r In Range("E2:E201")
st = Left(r.Text, 2)
If st <> "10" Then
r.EntireRow.Hidden = True
End If
Next r
End Sub