Add filter bars to a table - vba

If I have multiple tables in a single spreadsheet and both of them have filter bars, how can I check if one of them has filter bars and, if not, give it filter bars?
I tried a "If Not .AutoFilter Then" statement, but it's always false.
So this code always works:
With ActiveSheet.ListObjects(2).HeaderRowRange
If Not .AutoFilter Then
.AutoFilter
End If
End With
Keep in mind .AutoFilterMode, .FilterMode and .ShowAllData seem to work on whole sheets and not on tables.

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.

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"

Skip code when filters applied and returns no results VBA

I am using the below code to filter data as part of a much larger code however in some cases there may not be any results returned when the filters are applied. Alternatively If no results are returned I would like to skip part of the code.
Can anyone suggest the code I would need to use for this. (I'm guessing it would be an IF statement but unsure)
The code I am currently using is
Worksheets("DC Allocations Input").Activate
ActiveSheet.Unprotect Password:="Projects123"
ActiveSheet.Range("$B$9:$CT$382").AutoFilter Field:=1, Criteria1:="<>"
ActiveSheet.Range("$B$9:$CT$382").AutoFilter Field:=5, Criteria1:="=OPP", Operator:=xlOr, Criteria2:="=RSK"
ActiveSheet.Range("$B$9:$CT$382").AutoFilter Field:=6, Criteria1:="<>"
Any help would be much appreciated.
Thanks
You can use an if statement with special cells visible like this:
Dim lngCnt As Long
On Error GoTo someplace 'you get an error if there are no visible cells, use it to skip to where you want
lngCnt = Range("$b$10:$CT$382").SpecialCells(xlCellTypeVisible).Count 'Note I'm not including the headings row, this will always be visible
On Error GoTo 0
'Put your code to run if there are results from the filter here
someplace:
'And the code you want to resume at here

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"

VBA delete multiple rows after AutoFilter at once using xlCellTypeVisible is throwing error

I have an AutoFilter on a table that filters down the table properly where a column doesn't equal the string value provided. I then want to delete all of the visible rows that are left unfiltered (maybe 1000 to 2000 rows at once max). However I get an error on the EntireRow.Delete method. Here's my code, how can I successfully delete these rows all at once?
Dim columnFilter As String
columnFilter = "Foo"
Set tbl = ActiveCell.CurrentRegion
tbl.AutoFilter
tbl.AutoFilter Field:=columnNum, Criteria1:="<>" & columnFilter
'***Error thrown here
tbl.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
Error given is: Run-time error '1004': Delete method of Range class failed.
Don't use .Usedrange like this. Stick with the Range.CurrentRegion property instead and offset to retain the header.
There is also a caution that comes with deleting visible rows left visible after an AutoFilter Method has been implemented. You should always check to ensure that at least one row was left visible after the criteria has been applied before the delete operation. There are a number of ways to do that; this is one involving the native worksheet SUBTOTAL function's ability to exclude non-visible rows from its calculations.
Another issue is confusing a structured Table ListObject object with regular worksheet data. A structured table requires special consideration. I've added code in the routine below to test for the filtered area being a ListObject table and decide between two methods of deleting the rows based on that determination.
Sub fooFilter()
Dim columnFilter As String, columnNum As Long
columnFilter = "Foo"
columnNum = 1
With Sheets("Sheet1")
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
.AutoFilter Field:=columnNum, Criteria1:="<>" & columnFilter
With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
If CBool(Application.Subtotal(103, .Columns(columnNum))) Then
'very simple test to see if we are in a ListObject or not
If .ListObject Is Nothing Then
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
Else
.EntireRow.Delete
End If
End If
End With
'leave filter there but clear the filter
.AutoFilter Field:=columnNum
End With
End With
End Sub
If you are trying to delete all the way down to the bottom of the worksheet¹ you should get a
Run-time error: '1004'
Application-defined or object-defined error
The above error has nothing to do with limitations on how many rows you can delete. The problem is that you are trying to use Range.Offset property before resizing the associated range. Without resizing, you are trying to push the range off the sheet by the number of rows in the .Offset. .Resize to one row less first, then offset to save your header.
There is a certain volatility to the .UsedRange property and I use it less and less for anything at all.
There is a MS KB whitepaper on the problem with attempting to delete rows there none are showing. See Deleting or modifying rows that are above and below a hidden row in a filtered Excel 2007 worksheet also deletes or modifies the hidden row for details.
¹Having a worksheet legitimately populated to the very last row is very rare. It is much more likely that the worksheet is exhibiting a false xlCellTypeLastCell property due to a rogue value or over zealous formatting. Correcting the worksheet to properly reflect the extents of the actual data should come before adjusting coding to compensate for a problem with the worksheet.