'Contains' Filter with TextBox as User Input - vba

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

Related

Variable Multiple Criteria for Autofilter in VBA

I'm looking to use VBA autofilter to better sort my data as I work through it. I have around a 1000 rows each with a unique number and I'd like to be able to filter that data to the ID numbers I need at that moment. Basically, the autofilter code below does the job for those 5 specific entries, but is there a way of making that a more flexible?
ActiveSheet.Range("$A$13:$Y$1045").AutoFilter Field:=1, Criteria1:=Array( _
"1776", "1870", "2029", "2051", "2086"), Operator:=xlFilterValues
I picture using something along the lines of:
ActiveSheet.Range("$A$13:$Y$1045").AutoFilter Field:=1, Criteria1:=Array( _
TexBox2.Value, TextBox2.Value), Operator:=xlFilterValues
but no joy. I'm a bit of a newbie, so huge apologies if this is a huge waste of time. Many thanks in advance for any help!
I think you should be little more precise:
ActiveSheet.Range("$A$13:$Y$1045").AutoFilter Field:=1, Criteria1:=Array( _
UserForm1.TexBoxt1.Value, UserForm1.TextBox2.Value) _
, Operator:=xlFilterValues
where UserForm1 is the name of your userform. Be sure that you did not unloaded it before running this piece of code (you may .Hide it and still have access to the controls).

Excel VBA syntax for numeric wildcard in AutoFilter Criteria?

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"

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"

Excel VBA autofilter uncheck/exclude items

quick question, how can I exclude an item in a list through VBA. Have been working on a sheet that automatically prints out a list without a certain date on it.
Rows("2:2").Select
Selection.AutoFilter
ActiveSheet.Range("$A$3:$H$1000").AutoFilter Field:=3, Criteria1:="Hans"
ActiveSheet.Range("$A$3:$H$1000").AutoFilter Field:=7, Criteria1:="open"
ActiveSheet.Range("$A$3:$H$1000").AutoFilter Field:=6, Criteria1:="<>1/0/1900", Operator:=xlFilterValues
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
Rows("3:3").Select
Selection.AutoFilter
Problem is that the criteria does not work with the date 0-1-1900 to filter it out. What am I doing wrong?
0-1-1900 is a date that does not exist. This might be the problem.
Just use
Criteria1:=">1/1/1900"
and it should word fine.

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:="=*"