Filter a column based on its colour using VBA

justme101

New Member
Joined
Nov 18, 2017
Messages
34
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,526
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
With Sheets("Data")
   .Range("A1").AutoFilter 1, RGB(255, 0, 0), xlFilterCellColor
End With
 
Solution

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,301
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:

justme101

New Member
Joined
Nov 18, 2017
Messages
34

ADVERTISEMENT

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. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,486
Messages
5,636,611
Members
416,929
Latest member
Nitil

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
Top