Excel VBA syntax for numeric wildcard in AutoFilter Criteria? - vba

Example:
ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:=12345678
ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:=1234 & "*"
ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:=Array(12345678, 12345679, 12345670, ...)
My sample criteria are eight-digit values beginning with 1234. The first line works, but the second and third lines return a blank sheet. I've tried seemingly countless variations of the latter two lines, none of which have come to fruition. Thanks!

You can try the following workaround, as long as you have a consistant 8 digit structure, you can check if it's inside the value range of 12340000 and 12349999, like in the line below:
ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:=">=12340000", Operator:=xlAnd, Criteria2:="<=12349999"

Related

VBA Autofilter Using Multiple Criteria

All,
I am trying to filter on multiple criteria within VBA.
However I cannot find a simple way of doing this. The criteria I am selecting will always be constant but greater than 2 therefore I cannot use the or operator.
Code below;
Selection.AutoFilter field:=10, Criteria1:=Array("Fixtures & Fittings", "Furniture & Equipment", "Land & Buildings", "Motor Vehicles", "Plant & Machinery")
My current solution only filters on the last criteria within the array.
Due to the file set up I am unable to insert a formula in another column.
Thanks
You need Operator:=xlFilterValues.
Selection.AutoFilter field:=10, Operator:=xlFilterValues, Criteria1:=Array("Fixtures & Fittings", "Furniture & Equipment", "Land & Buildings", "Motor Vehicles", "Plant & Machinery")
If the criteria is constant, I would assume that the filter range is consistent as well. You may wish to move away from using Selection in the near future. See How to avoid using Select in Excel VBA.

Add filter bars to a table

If I have multiple tables in a single spreadsheet and both of them have filter bars, how can I check if one of them has filter bars and, if not, give it filter bars?
I tried a "If Not .AutoFilter Then" statement, but it's always false.
So this code always works:
With ActiveSheet.ListObjects(2).HeaderRowRange
If Not .AutoFilter Then
.AutoFilter
End If
End With
Keep in mind .AutoFilterMode, .FilterMode and .ShowAllData seem to work on whole sheets and not on tables.

VBA AutoFilter hiding all rows - including the ones matching criteria

I'm applying VBA AutoFilter to some results in an excel sheet. It seems to compile properly, but when I check the results, the AutoFilter is hiding both the rows that match and that do not match the criteria I applied.
If I manually select the autofilter that was applied, i see that the criteria that I coded is correctly input and, by just clicking enter, the criteria matching rows show.
I'm using a Brazilian Portuguese version of Excel, not sure if that might be the issue.
Here's what I've tried:
Sub FilterOff(ByVal thisSheet)
thisSheet.Activate
With thisSheet
.AutoFilterMode = False
.Range("A1:F1").AutoFilter
.Range("A1:F1").AutoFilter Field:=4, Criteria1:=">0.01", _
Operator:=xlOr, Criteria2:="<-0.01"
.Range("A1:F1").AutoFilter Field:=5, Criteria1:=">100"
End With
End Sub
I was experiencing something similar in one of my macros. I had a table that I was trying to autofilter. I could do it manually, but not in VBA, even when I was exactly replicating what the recording function gave me. I also could not copy+paste as values in VBA, but I could manually.
What worked for me was to save and close the workbook, then reopen it and apply the autofilter. Specifically, I used this:
tempWb.SaveAs ("dir\temp.xlsx")
tempWb.Close (0)
Set rptWb = Workbooks.Open("dir\temp.xlsx")
Set rptWs = rptWb.Sheets(1)
rptWs.Range(rptWs.Cells(1, 1), rptWs.Cells(lstRow, lstCol)).AutoFilter Field:=20, Criteria1:="=NO RECORD"
and it worked.
Update: I think the underlying issue was that I had calculation set to manual. After I set calculation to automatic, the problems went away.
I did something like this and it worked
Range("A1:B6").AutoFilter
ActiveSheet.Range("$A$1:$B$6").AutoFilter Field:=1, Criteria1:="=10", _
Operator:=xlOr, Criteria2:="=30"
ActiveSheet.Range("$A$1:$B$6").AutoFilter Field:=2, Criteria1:="100"

How to edit a macro in excel 2010 so that the filter criteria is: not equal to blanks?

I'm new to creating macros so my apologies if my question is not very clear. I have recorded a macro do autofilter a spreadsheet. What I need it to do is to not include the blanks in the filter. Considering that each sheet has different values inputted the way the macro is setup right now is based on the first sheet I used.
This is what the code looks like:
Range ("B7:B8) .Select
Selection.AutoFilter
ActiveSheet.Range("$B$7:$N$38").AutoFilter Field:=12, Criteria1:=Array( _
"1.00", "13.00", "2.00", "3.00", "3.50", "42.50", "6.00", "7.00", "Total"), Operator _
:=xlFilterValues
So what I think it should be is changing the Criteria in order for it to not select blanks instead of selecting exact values (that will be different in every sheet).
Does anyone know how to do this? and please let me know if you need any other information.
Thank you.
How to edit a macro in excel 2010 so that the filter criteria is: not equal to blanks?
in continuation to my comment, what if the value is <0?
The best way is to use this
Criteria1:="<>"
FOLLOWUP FROM COMMENTS
Try this
Yourrng.AutoFilter Field:=12, Criteria1:="<>", Criteria2:="<>-"
I did this
Myrng.AutoFilter Field:=12, Criteria1:="<>", Criteria2:="<>0"
For number you can use:
Criteria1:=">0"
For Text you could use:
Criteria1:="=*"

'Contains' Filter with TextBox as User Input

I'm guessing this is simple but I'm struggling to find a proper solution to this.
I require to use the 'Contains' filter with a TextBox as the user input.
Eg: User types in "Hello" in the TextBox and results returned are "Hello USA", "Hello Buddy", "Hello" etc.
The piece of code which I'm stuck with
Selection.AutoFilter Field:=1, Criteria1:=UserForm1.TextBox1.Value, Operator:=xlOr
Right now it just gives me cells with the exact word in it.
Could anyone point me in the right direction or a tutorial link.
Thanks for your time.
When in doubt, record a macro using the macro recorder. Which will give you:
Selection.AutoFilter Field:=1, Criteria1:="=Hello*", Operator:=xlAnd
Therefore,
Selection.AutoFilter Field:=1, Criteria1:="=" & UserForm1.TextBox1.Value & "*", Operator:=xlAnd