ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Code to capture filter settings works but not for color filters (https://www.excelbanter.com/excel-programming/450987-code-capture-filter-settings-works-but-not-color-filters.html)

Phrank

Code to capture filter settings works but not for color filters
 
Hi. Below is a snippet of code I came across on the internet that
works great for recording the current filter settings, allowing code
to run, and then resetting the filters, for anything filtered on
values at least. However, if a column is filtered by color, the macro
errors out. I'm fairly sure the error revolves around the Criteria or
Operator, but why would there be a difference, and what might I be
able to do to incorporate the color filtering so it doesn't error out?
Thanks!

wksQB.Select

' Capture AutoFilter settings
With wksQB.AutoFilter
currentFiltRange = .Range.Address
With .Filters
ReDim filterArray(1 To .Count, 1 To 4)
For f = 1 To .Count
With .Item(f)
If .On Then
filterArray(f, 1) = .Criteria1
If .Operator Then
filterArray(f, 2) = .Operator
End If
End If
End With
Next f
End With
End With

'Remove AutoFilter
wksQB.AutoFilterMode = False

' my other code goes here

RestoreFilters:
' Restore Filter settings
For col = 1 To UBound(filterArray(), 1)
If Not IsEmpty(filterArray(col, 1)) Then
If filterArray(col, 2) Then
wksQB.Range(currentFiltRange).AutoFilter Field:=col, _
Criteria1:=filterArray(col, 1), _
Operator:=filterArray(col, 2), _
Criteria2:=filterArray(col, 3)
Else
wksQB.Range(currentFiltRange).AutoFilter Field:=col, _
Criteria1:=filterArray(col, 1)
End If
End If
Next col


Here are 4 filters I've applied. All work with the code above to
unfilter and refilter, except the Criteria1:=RGB(0,176,80) with
Operator:=xlFilterCellColor.

Examples. Below shows 4 filters. All work except the
Operator:=xlFilterCellColor
ActiveSheet.Range("$A$1:$CI$3809").AutoFilter Field:=6,
Criteria1:= _ ' This works
"Bell, Francis"
ActiveSheet.Range("$A$1:$CI$3809").AutoFilter Field:=16,
Criteria1:=Array( _
"RemB Handswitch", "REMB HANDSWITCH - EUROPEAN", "Universal
Handswitch"), _
Operator:=xlFilterValues
ActiveSheet.Range("$A$1:$CI$3809").AutoFilter Field:=1,
Criteria1:="45", _
Operator:=xlAnd
ActiveSheet.Range("$A$1:$CI$3809").AutoFilter Field:=2,
Criteria1:=RGB(0, _
176, 80), Operator:=xlFilterCellColor


All times are GMT +1. The time now is 01:44 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com