Autofiltering two columns as an OR function - vba

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.

Related

Filtering Pivot Table column

I tried and looked on this forum and Others but I couldn't find the right working code yet.
I want to filter on a pivot table a column with values equal or higher than 10%.
When I recorded my macro, code was as follow:
Sub Macro9()
Macro9 Macro
ActiveSheet.Range("$A$5:$M$36607").AutoFilter Field:=13, Criteria1:=">=0.1" _
, Operator:=xlAnd
End Sub
This doesn't work but I wanted to launch the macro afterwards.
So I tried other codes, like the following :
Sub FilterPivotTable()
Application.ScreenUpdating = False
ActiveSheet.PivotTables("PivotTable1").ManualUpdate = True
ActiveSheet.PivotTables("PivotTable1").PivotFields("Name of my column").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Name of my column").PivotFilters. _
Add Type:=xlCaptionEquals, Value1:=">=0.1"
ActiveSheet.PivotTables("PivotTable1").ManualUpdate = False
Application.ScreenUpdating = True
End Sub
But this didn't work either.
Can anyone please help me?
Thank you very much in advance.
EDIT: Apparently, the fact that my field is a calculated one is important. Still don't have an answer though.
So, I found myself an answer to my question.
Since the columns I wanted to filter were values and calculated fields, I actually needed to filter these columns by right-clicking on a cell inside a row value of my PVT. Then, I had to filter by chosing the right value to sort and add my criteria.
The code when recording a macro is as follow:
ActiveSheet.PivotTables("PivotTable1").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Your row name").PivotFilters. _
Add2 Type:=xlValueIsGreaterThanOrEqualTo, DataField:=ActiveSheet. _
PivotTables("PivotTable1").PivotFields("Your value field"), Value1:=0.1
Hope it can help some of you, as I haven't seen an answer to this issue in many forums.
Have a nice day!

How can I autofilter when I'm not sure which column it will be?

I'm trying to automate the processing of various reports, which I just need to filter and count the rows of. I currently have a PowerShell script that opens each report and runs a few macros. It is just about in a working state, but I'd now like to make it a bit more intelligent and catch some of the fails, so I might be asking a few questions but I'll stick to the one problem on each.
The reports have similar, but not identical layouts. I am looking for a particular column name to then autofilter. I have a very basic and bodged together macro that currently does this, and works (most of the time) for example, sometimes the column I want is A or B:
If Worksheets(1).Range("A1") Like "*word" Then
Worksheets(1).Range("A1").AutoFilter Field:=1, Criteria1:="=criteria1", Operator:=xlOr, Criteria2:="=criteria2"
ElseIf Worksheets(1).Range("B1") Like "*word" Then
Worksheets(1).Range("A1").AutoFilter Field:=2, Criteria1:="=criteria", Operator:=xlOr, Criteria2:="=criteria2"
Hopefully that gives you the current picture.
I now want to instead, do a search for the field header I am looking for, then filter that column, so if the report format in future changes my macro won't break. Something similar to:
ColNum = Application.Match("*header", Range("A:Z"), 0)
ColNumInt = CInt(ColNum)
If ColNumInt > 0 Then
ActiveSheet.Range("A1").AutoFilter Field:=ColNumInt, Criteria1:="=criteria1*", Operator:=xlAnd
End If
But this gives an error "AutoFilter method of Range class failed", Googlefu says to turn off filters but they're already off. So I'm a bit stuck.
This part will always fail:
ColNum = Application.Match("*header", Range("A:Z"), 0)
since match only works on one row or column. So your code is actually returning Error 2042, which is then converted to 2042 by CInt. I guess you don't have that many columns of data, hence the autofilter fails. Use:
ColNum = Application.Match("*header", Range("A1:Z1"), 0)
If Not IsError(ColNum) Then
...
End If
This should work for you.
Sub Button1_Click()
Dim r As Range
Dim c As Integer
Set r = Range("A1:B1").Find(what:="*word*", lookat:=xlWhole)
c = r.Column
ActiveSheet.AutoFilterMode = 0
Columns(c).AutoFilter Field:=1, Criteria1:="*criteria1*"
End Sub

Excel VBA - AdvancedFilter

I am trying to filter a range dynamically in VBA and the VBA I am using is not working but I cannot see a logical reason as to why. To explain, I have a range of data in a sheet entitled "Full Stock Report" the size of which will change but I've set it statically in this example... And I'm trying to filter it by a list of criteria held in a range on a sheet initiated "Spitfire Aval Locations", again this is also dynamic but I've set as static again in this example. This sounds simple to me but the below line of code applies a filter but with no results (I have checked I know there are lots that should appear from this filter).
My second question is related, how does this VBA statement dictate which column in the range is being filtered ? (I fear this may be my issue ....)
Sheets("Full Stock Report").Range("A1:F20623").AdvancedFilter Action:=xlFilterInPlace,
CriteriaRange:=Sheets("Spitfire Aval Locations").range("A2:A228"), Unique:=False
Think I've solved this ... essentially AdvancedFilter requires the criteria to be the same format and same column titles as your data set. Not hugeley helpful to me, but I can bodge it to work.
I also have a hunch that AutoFilter with specified criteria might be a better option...
The column to filter on is the first .Range that you call .AdvancedFilter on. The code you posted filters columns A through F. If you wanted to only filter based on values in column A, it would look more like this:
Sheets("Full Stock Report").Range("A1:A20623").AdvancedFilter _
Action:=xlFilterInPlace, _
CriteriaRange:=Sheets("Spitfire Aval Locations").Range("A2:A228"), _
Unique:=False

Excel Macro Autofilter issue with variable

I have a table of data with the top row being filters, I have a loop that changes which filter needs to be used inside the loop is the variable filterColumn that is being assigned a new value every time the loop runs through.
when i try to use filterColumn to determine which filter will be 'switched on' i get an error
Autofilter method of Range Class Failed
ActiveSheet.Range("$U$83:$CV$1217").AutoFilter Field:=filterColumn, Criteria1:="<>"
What is the correct syntax in order to use a variable to determine which field the filter is in?
Problem Solved I found the solution. I was referencing the filters columns position in terms of the whole worksheet when in fact I should have been referencing what number it was in the group of filters. For example the filter I wanted to change was in 'CF' which is the 84th column but my the filter I wanted to change is the 64th in the group.
Dim filterColumn As Integer
filterColumn = 2
ActiveSheet.Range("$U$83:$CV$1217").AutoFilter Field:=filterColumn, _
Criteria1:="<>"
EDIT: I tried #HeadofCatering's solution and initially it failed. However I filled in values in the referenced columns and it worked (my solution also failed under reverse conditions - make the column headers blank and it fails).
However this doesn't quite mesh with what I've (and probably you've) seen - you can definitely add filters to columns with blank headers. However one thing was consistent in the failures I saw - the filterColumn referenced a column that was outside of Application.UsedRange. You may want to try verifying that the column you are referencing is actually within Application.UsedRange (easy way: run Application.UsedRange.Select in the Immediate Window and see if your column is selected). Since you are referencing a decent amount of columns, it is possible that there are no values past a certain point (including column headers), and when you specify the column to filter, you are actually specifying something outside of your UsedRange.
An interesting (this is new to me as well) thing to test is taking a blank sheet, filling in values in cells A1 and B1, selecting columns A:G and manually adding AutoFilters - this will only add filters to columns A and B (a related situation can be found if you try to add filters to a completely blank sheet).
Sorry for the babble - chances are this isn't even your problem :)
Old solution (doesn't work when conditions described above are used)
I may be overkilling it, but try setting the sheet values as well (note I used a sample range here):
Sub SOTest()
Dim ws As Worksheet
Dim filterColumn As Integer
' Set the sheet object and declare your variable
Set ws = ActiveSheet
filterColumn = 2
' Now try the filter
ws.Range("$A$1:$E$10").AutoFilter Field:=filterColumn, Criteria1:="<>"
End Sub

Getting a total copied set of rows in VBA and storing it in a variable

I asked this question a bit ago, but asked it incorrectly, and wasn't able to receive the best answer.
I have a fairly simple syntax question:
I'm trying to copy and paste n rows from one excel file to another. In addition, I'd like to store the total FILTERED copied rows into a variable. Can someone help me accomplish this?
For example:
'1) Activate CSV file, Apply Filter to Column B (Page Title) & uncheck "blanks" ("<>") filter
Windows("Test_Origin.xlsm").Activate
ActiveSheet.Range("$A$1:$J$206").AutoFilter Field:=2, Criteria1:="<>"
'2) Copy Filtered Lines with data (Excluding Row 1)
Range("B2:F189").Select
Selection.Copy
copiedRowTotal = Selection.Rows.Count <-- This doesn't give me the FILTERED rows
Thanks
dim i as long
i = Application.WorksheetFunction.Subtotal(2,worksheets("Sheet").Range("B2:F189"))
Gave you the description here
Getting a total copied set of rows in VBA and storing it in a variable
Try:
copiedRowTotal = Range("B2:B189").SpecialCells(xlCellTypeVisible).Count