Variable Multiple Criteria for Autofilter in VBA - 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).

Related

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 Macro not sorting correctly

I have this script and it sorts MT-001 - MT-999 recently I added two more entries "MT-1000" and "MT-1001" and this is how my Macro is sorting.....
MT-099
MT-100
MT-1000
MT-1001
MT-102
MT-103
My macro for sorting is the following:
Sub FILTERMT()
Selection.AutoFilter Field:=2, Criteria1:="=*MT*", Operator:=xlAnd
Range("A7:BZ65536").Sort Key1:=Range("B6"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub
Can someone post a code that corrects this error? THX for contributing to this community!
If you notice, MT-099 has a leading zero to ensure it sorts before MT-100 while MT-102 does not have a leading zero to ensure it sorts before MT-1000.
You will need to modify your data to support 4-digit, left zero-padded numbers, or come up with a way to split the numeric portion off into a different column for sorting purposes.
Choose a 3rd column, that may even be a many columns to the right if necessary.
Fill the cells of this column with this formula:
=RIGHT(B6;LEN(B6)-FIND("-");B6))*1
Here, I assume that the top of your list ("MT-099") ist at field B6.
The right expression cuts the number out of the string. The *1 is to really convert to a number. You need only the number treated as a number in order to sort the right way.
Se the formula e.g. in C6 and copy the content downwards to the end row of your list.
Then simply alter your code; here I assume that your new column is C:
Range("A7:BZ65536").Sort Key1:=Range("C6"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
To clarify, here is how it looks like after sorting:
If necessary, you may narrow your 3rd column to zero length, so it is invisible.

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

VBA - "does not contain"-filter (excel 2010) [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Combine multiple exclusion (<>) criteria in AutoFilter
I'm trying to set up a filter with multiple 'Does not containt' values. I seem to get an error doh every time I try to run it. I've looked up this error and it means that excel didn't find any value's. When I try to run the same code, only with one value, it works fine. Any help/suggestions?
Code with multiple values:
ActiveSheet.Range("$A$1:$J$218").AutoFilter Field:=5, Criteria1:=Array("<>*a*", "<>*b*", "<>*c*"), Operator:=xlFilterValues
Code with only one value:
ActiveSheet.Range("$A$1:$J$218").AutoFilter Field:=5, Criteria1:="<>*a*", Operator:=xlFilterValues
Greetz
Bulki
Have you tried this?
ActiveSheet.Range("$A$1:$J$218").AutoFilter Field:=5, Criteria1:="<>*a*", Operator:=xlAnd, Criteria2:="<>*b*", Operator:=xlAnd, Criteria3:="<>*c*", Operator:=xlFilterValues
Failing that, have you tried recording what you want as a Macro as copying/adapting the recorded code to do what you want?

'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