A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Because you were setting the pattern in the code I assumed that there would be no pattern in the cells with the black interior fill. If the cells contain the pattern when set to black then the interior fill remains even though it cannot be seen. Therefore the filter must be set to the black color and then modified to include the pattern.
I could not be certain that you will not have both combinations. ie some black without pattern and some black with pattern. Therefore I have modified the code as follows to accommodate either or both conditions.
Set filter to just black and if any visible rows then assign to rng variable.
Add the pattern to the Criteria1 filter and if any visible rows then add that to the rng variable. (At this point rng is tested for Not Nothing because if it already contains the just black range then need to use Union to add the black plus pattern range or if rng is nothing then just assign the black plus pattern range.)
Replace my previous code with the following code. Note: See my comments where I have altered the method of testing if any visible data so do not need the On Error routine.
Application.GoTo Worksheets("Client").Range("B3")
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:=RGB _
(0, 0, 0), Operator:=xlFilterCellColor
Dim rng As Range 'rng initializes to Nothing when Dimmed so no need to re-initialize to Nothing
With ActiveSheet.AutoFilter.Range
'Test if more than 1 row (more than 1 row is more rows than just header)
If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
'Assign visible data to rng
Set rng = .Offset(1, 0) _
.Resize(.Rows.Count - 1, .Columns.Count) _
.SpecialCells(xlCellTypeVisible)
End If
End With
'Reset filter to include pattern
With ActiveSheet.ListObjects("Table1").AutoFilter.Filters(1).Criteria1
.Pattern = xlGray16
.PatternColor = 0
.Color = 0
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With ActiveSheet.AutoFilter.Range
'Test if more than 1 row (more than 1 row is more rows than just header)
If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
'Assign visible data to rng.(Add to range if rng already contains a range)
If Not rng Is Nothing Then
Set rng = Union(rng, .Offset(1, 0) _
.Resize(.Rows.Count - 1, .Columns.Count) _
.SpecialCells(xlCellTypeVisible))
Else 'If rng is Nothing. ie does not contain a range.
Set rng = .Offset(1, 0) _
.Resize(.Rows.Count - 1, .Columns.Count) _
.SpecialCells(xlCellTypeVisible)
End If
End If
End With
ActiveSheet.ShowAllData
If Not rng Is Nothing Then 'Not nothing then contains a range
With rng.Interior
.Pattern = xlGray16
.PatternColor = 0
.Color = 16777215
.TintAndShade = 0
.PatternTintAndShade = 0
End With
rng.ClearContents
Else 'Optional code used during testing. Can be deleted after testing
MsgBox "No visible data" 'Optional code used during testing. Can be deleted after testing
End If
' ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1 'Not required. ShowAllData above