Excel VBA autofilter uncheck/exclude items - vba

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.

Related

excel error 1004 autofilter method of range class failed

I am trying to use the autofilter method in a macro for some reason excel fails when calling the autofilter method. I get excel error 1004 autofilter method of range class failed. This is the line where it fails
Range("S2").Select
Range(Selection, Selection.End(xlDown)).AutoFilter Field:=1, Operator:= _
xlFilterValues, Criteria2:=Array(0, "1/0/1900")
This is already a changed version originally my code looked like this but I got the same error.
ActiveSheet.Range("$A$1:$W$" & Rows.Count).AutoFilter Field:=19, Operator:= _
xlFilterValues, Criteria2:=Array(0, "1/0/1900")
I tried this aswell but it also didn't work
ActiveSheet.Range("A:W").AutoFilter Field:=19, Operator:= _
xlFilterValues, Criteria2:=Array(0, "1/0/1900")
To the comment telling me the date is not valid. I am pretty sure it is. Thats how the date is entered in the worksheet I got from my coworker
[1]: https://i.stack.imgur.com/n1fDP.png
That's German Date Format but it is the same as 1/0/1990. I can not show more of the table cause of company rules

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

How to create Pivot from selection, in VBA, Excel

I tried analyzing how Excel creates a pivot from a selection. It seems pretty simple. I select the entire range in my document and hit Create Pivot with that range. That produces the following code:
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Keyword report!R2C1:R643841C25", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="Sheet38!R3C1", TableName:="PivotTable2" _
, DefaultVersion:=xlPivotTableVersion15
I want to use this code in the future, but instead of using a fixed reference to a particular range, I want Excel to draw that data from the Selection. For some reason, that's not included by the Macro Record. I have no idea what "R2C1:R643841C25" is or how it is derived. My selection is: A1:Y643841.
The big question: How do I replace R2C1:R643841C25 in the code with the "Selection"?
An understanding of R1C1 notation would help you answer this.

ActiveSheet.AutoFilter.Sort.SortFields.Clear in Excel 2003

I have a macro that works in Excel 2013, but the following part of the code breaks when running the macro in Excel 2003:
Sheets("dados").Select
Range("A1").AutoFilter Field:=6, Criteria1:="<>"
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveSheet.AutoFilter.Sort.Apply
I wasn't able to find a clear reason why it is breaking. I read people mentioning the problem is the Sort object, but didn't find any replacement options. Is there a replacement for this filtering procedure which would work in Excel 2003?
I appreciate any help.
Try this one:
With ThisWorkbook.Sheets("dados")
.Range("A1").AutoFilter Field:=6, Criteria1:="<>"
.Range("A1").CurrentRegion.Sort Key1:=.Range("A1"), Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, DataOption1:=xlSortNormal
End With

'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