Filter a column based on its colour using VBA

justme101

Board Regular
Joined
Nov 18, 2017
Messages
67
Office Version
  1. 365
Platform
  1. Windows
Hello Folks,

I have a code which is supposed to colour all repeating values in a column with a particular colour and then filter only those cells which are coloured. The colouring part works fine, but I am not able to filter the cells which are coloured. Here's the code:

Dim myRange As Range
Dim i As Integer
Dim j As Integer
Dim myCell As Range
Set myRange = Range("A:A")

For Each myCell In myRange
If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
myCell.Interior.ColorIndex = 3
End If
Next

Dim cel As Range, rng As Range
Set rng = Sheets("Data").Range("A1", Sheets("Data").Range("A65536").End(xlUp).Offset(, 1))

'** Start AutoFilter Process
With Sheets("Data").Rows("1:65536")
.AutoFilter
'** Use temporary column 1
.AutoFilter Field:=1, Criteria1:="Interior.ColorIndex = 3"
End With

Requesting your experience on this. Thank you.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about
VBA Code:
With Sheets("Data")
   .Range("A1").AutoFilter 1, RGB(255, 0, 0), xlFilterCellColor
End With
 
Upvote 0
Solution
Try:
VBA Code:
Sub FilterByColour()
    Application.ScreenUpdating = False
    Dim myRange As Range
    Set myRange = Range("A:A")
    With Sheets("Data")
        For Each myCell In .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
            If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
                myCell.Interior.ColorIndex = 3
            End If
        Next myCell
        .Range("A1").AutoFilter Field:=1, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor
    End With
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
W
Try:
VBA Code:
Sub FilterByColour()
    Application.ScreenUpdating = False
    Dim myRange As Range
    Set myRange = Range("A:A")
    With Sheets("Data")
        For Each myCell In .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
            If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
                myCell.Interior.ColorIndex = 3
            End If
        Next myCell
        .Range("A1").AutoFilter Field:=1, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor
    End With
    Application.ScreenUpdating = True
End Sub
This works, but I already marked the previous reply as the answer. Thank you so much for your time though. :)
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,435
Members
448,898
Latest member
dukenia71

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top