Filter by colour

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Filter by colour

Post by adam »

Hi anyone,

How could I write a VB code that would filter rows of column O that is highlighted in yellow?
My data starts from row 9 where headers in row 8.

Any help on this would be kindly appreciated.

Thanks in advance.
Best Regards,
Adam

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Filter by colour

Post by Rudi »

You can adapt this to suite...

Code: Select all

Sub FilterData()
Dim rgData As Range

Const sCriteria = "Test" '<< Change the criteria to filter for...
    
    Set rgData = Range(Cells(8, "O"),Cells(Rows.Count, "O").End(xlUp))
    rgData.AutoFilter 1, sCriteria
    'rgData.Parent.AutoFilterMode = False '<< Remove filter if needed...
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Filter by colour

Post by HansV »

To filter by color:

Code: Select all

Sub FilterData()
    Dim rng As Range
    Dim m As Long
    m = Range("O" & Rows.Count).End(xlUp).Row
    Set rng = Range("O8:O" & m)
    rng.AutoFilter Field:=1, Criteria1:=vbYellow, Operator:=xlFilterCellColor
End Sub
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Filter by colour

Post by Rudi »

    
Arghhh!!!
Completely forgot about the part to filter by colour! :stupidme:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Filter by colour

Post by adam »

Thanks for the help Rudi & Hans. Since I also have data in columns C to column P I've changed the field line from Field:=1 to Field:=13 so that it would filter column O.
Best Regards,
Adam