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

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?

Related

Autofiltering two columns as an OR function

I am trying to filter two columns in excel to show all the results that deliver today(two separate columns). I have tried multiple ways, however I keep getting results that reflect an "AND" statement. I feel like I am close with.
Sub Playing_Today_v2()
Dim s As String
s = "=" & CStr(Date)
With ActiveSheet.ListObjects("table1").Range
.AutoFilter
.AutoFilter Field:=7, Criteria1:=s
End With
With ActiveSheet.ListObjects("table1").Range
.AutoFilter
.AutoFilter Field:=9, Criteria1:=s
End With
End Sub
Any and all help is greatly appreciated!!
Hello in order to do this via VBA you will first need to understand how to accomplish it manually 2 ways that come to mind are:
Add a helper column to the table
Use an advanced filter
For the helper column add a formula similar to =OR(Col7=TODAY(),Col9=TODAY()) and filter to that column equals true.
For the advanced filter create a 2 x 3 range and populate as follows:
Col7 NAME, Col9 NAME
=Today() ,
, =Today()
As use that as your criteria range to do a OR selection (if you want an AND selection put them on the same line).
Once you have got this working manually it is fairly easy to move to VBA either by adding a new column and adding 1 autofiler similar to your existing code or by using the Range.AdvancedFilter method.
If you get stuck with the code please post a question showing how far you get and someone will be able to help.

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).

1004 error when trying to run a macro that applies to a table

I have an existing table. I want to add a new column next to it, with a formula in it so that the formula adds a new column and fills in all the values.
This is a two-page worksheet. Table1 is on another page than the table we're working with, but has the master data it draws from.
I recorded a macro of the formula I used to produce this, but when I try to run it, I get
Run time error '1004': Application-defined or object-defined error.
The code I'm using is below:
Range("B2").Select
ActiveCell.FormulaR1C1 = "=IF(INDEX(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Status"",Table1[#Headers],0))=""for sale"",""seller""," & _
"IF(INDEX(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Status"",Table1[#Headers],0))=""for lease"",""landlord""," & _
"IF(INDEX(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Status"",Table1[#Headers],0))=""for sale or lease"",""seller / landlord""" & _
"X(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Base Rent/Mo"",Table1[#Headers],0))>0,""landlord"",""seller""))))"
Range("B1").Select
Things I've checked so far:
All the names match (i.e. Table1 is a valid name, and all the headers are correctly named)
The formula works exacly as I want to if I just type it in and hit 'enter'
I've looked for similar issues here, and the most similar seem to be some problems with Pivot tables, but the solutions don't seem to be applicable to my problem
(The reason I'm using VBA for this is that this is one part of a multi-step process that I'm trying to automate to make it simpler to run a complicated report from a large set of data.)
EDIT: Additional fixes I tried:
Moving the master data table to the same sheet as my 'target' table to see if it would work if the two were on the same worksheet. No go.
When you need to use quotes inside quotes like in "MATCH(""Status"", which I suppose you are trying to output Match("Status" try using "MATCH("&Chr(34)&Status&Chr(34)
Chr(34) outputs the " symbol. Otherwise it would return MATCH(Status (without quotes)
There were two separate issues.
First, I was recording the macro and apparently, Excel will not record formulas longer than a certain length properly. This limit is greater than 407 (the longest formula I got to auto capture), but shorter than 467 characters. A similar problem was discussed on this Mr. Excel post. This also was why part of the "INDEX" was missing, as BruceWayne pointed out.
In addition, it was necessary to switch from .FormulaR1C1 to .Formula to get it to work correctly, as R3uK had suggested.
The final code ended up as follows:
Range("B2").Select
ActiveCell.Formula = _
"=IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Status"",Table1[#Headers],0))=""for sale"",""seller"",IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Status"",Table1[#Headers],0))=""for lease"",""landlord"",IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Status"",Table1[#Headers],0))=""for sale or lease"",""seller / landlord"",IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Base Rent/Mo"",Table1[#Headers],0))>0,""landlord"",""seller""))))"
Range("B3").Select

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