VBA - autofilter - always hide exact value - vba

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

Related

Make search field in VBA Excel dynamicly focus results

I have a large Excel sheet and to that I have added a dynamic search field textbox and that works fine.
Private Sub TextBox1_Change()
ActiveSheet.Range("E6:E150").AutoFilter Field:=4, Criteria1:="*" & [G1000] & "*", Operator:=xlFilterValues
End Sub
The problem is that if I filter out say row number 500 the result is not visible so I have to manually move the cursor up.
I tried this (and a lot of other stuff) without success.
Private Sub TextBox1_Change()
With ActiveSheet.Range("E6:E150").AutoFilter Field:=4, Criteria1:="*" & [G1000] & "*", Operator:=xlFilterValues
.Select
End With
End Sub
It yield this
"run-time error '424': Object required"
Any help is appreciated.
Perhaps the first visible cell above the currently selected but hidden activecell.
Private Sub TextBox1_Change()
with ActiveSheet
.Range("E6:E150").AutoFilter Field:=1, Criteria1:="*" & [G1000] & "*"
if activecell.EntireRow.hidden then
dim i as long
for i=activecell.row to 6 step-1
if not .cells(i, activecell.column).EntireRow.hidden then
.cells(i, activecell.column).select
exit for
end if
next i
end if
end with
End Sub
I've changed the AutoFilter field to 1 as there aren't 4 fields in Range("E6:E150"); there is only 1. Also removed the unnecessary Operator:=xlFilterValues as that is only required when using an array as Criteria1.
Sorry for being unprecise.
Using select is no good idea I can see from implementing Jeepeds answer.
I found what I'm after:
Private Sub TextBox1_Change()
With ActiveSheet
.Range("E6:E150").AutoFilter Field:=4, Criteria1:="" & [G1000] & ""
ActiveWindow.ScrollRow = 1
End With
End Sub
Just that simple.

VBA not looping filtered rows

I have this big excel file that is automated to do some processes. Today, I figured there is an issue with one of the columns and I need to fix it. to fix it I am generated this code below to filter column N to show all '#N/A'. with the filtered rows, I want to check and see if the offset to the right 2 columns has the value "Available". if it does, I want to loop through all column N and replace the '#N/A' with 'Unkown'. but the code I generated only works for the first filtered cell and doesn't loop.
Sub tess()
ActiveSheet.Range("$C$1:$AR$468").AutoFilter Field:=12, Criteria1:="#N/A"
ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(, 12).Select
Dim lr
lr = ActiveSheet.UsedRange.Rows.CountLarge
For Each cell In Range("n1:n" & lr)
If ActiveCell.Value = CVErr(xlErrNA) And ActiveCell.Offset(, 2).Value = "Available" Then
ActiveCell.Value = "Unkown Person"
End If
Next cell
End Sub
Thank you.
you can avoid looping by adding another filter:
Sub tess()
With ActiveSheet.Range("$C$1:$AR$468")
.AutoFilter Field:=12, Criteria1:="#N/A"
.AutoFilter Field:=14, Criteria1:="Available"
If Application.WorksheetFunction.Subtotal(103, .Columns(12)) > 1 Then .Offset(1, 11).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible).Value = "Unkown Person"
.Parent.AutoFilterMode = False
End With
End Sub
You should refer to "cell" within your For loop, not "ActiveCell".

Referencing multiple cells VBA Autofilter

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

Filter to exclude formula and blank

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.

Filter numeric field

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