Filter rows containing specific text

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

Filter rows containing specific text

Post by adam »

Hi anyone,

I'm trying to figure out a code that would filter the sheet "Orders" depending on the value in column "M".

Lets say for example when I click the button the code would filter all the rows containing "apple".

Any help on this would be kindly appreciated.

Thanks in advance.
Best Regards,
Adam

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

Re: Filter rows containing specific text

Post by HansV »

Why don't you use AutoFilter? No code needed, and anyone can use AutoFilter.
Best wishes,
Hans

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

Re: Filter rows containing specific text

Post by adam »

I do respect your suggestion Hans. But my intention is to filter the rows that contain the text "apple" in column "N" when the button is clicked.
Best Regards,
Adam

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

Re: Filter rows containing specific text

Post by HansV »

Where does the user enter the word "apple"?
Best wishes,
Hans

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Filter rows containing specific text

Post by Don Wells »

adam wrote:Hi anyone,

I'm trying to figure out a code that would filter the sheet "Orders" depending on the value in column "M".

Lets say for example when I click the button the code would filter all the rows containing "apple".

Any help on this would be kindly appreciated.

Thanks in advance.
The following code should be self explanatory.

Code: Select all

Sub Filter4Apple()
    Range("M1").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=113, _
      Criteria1:="=*apple*", Operator:=xlAnd
End Sub
Sub RemoveFilter()
    Range("M1").Select
    Selection.AutoFilter
End Sub
Code modified to filter on M
Regards
Don

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Filter rows containing specific text

Post by Don Wells »

Don Wells wrote:
adam wrote:Hi anyone,

I'm trying to figure out a code that would filter the sheet "Orders" depending on the value in column "M".

Lets say for example when I click the button the code would filter all the rows containing "apple".

Any help on this would be kindly appreciated.

Thanks in advance.
The following code should be self explanatory.

Code: Select all

Sub Filter4Apple()
    Range("M1").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=113, _
      Criteria1:="=*apple*", Operator:=xlAnd
End Sub
Sub RemoveFilter()
    Range("M1").Select
    Selection.AutoFilter
End Sub
Code modified to filter on M
Oops! That line

Code: Select all

    Selection.AutoFilter Field:=113, _
      Criteria1:="=*apple*", Operator:=xlAnd
should have read

Code: Select all

    Selection.AutoFilter Field:=13, _
      Criteria1:="=*apple*", Operator:=xlAnd
    13 not 113
Sorry :gramps:
Regards
Don

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

Re: Filter rows containing specific text

Post by adam »

The code does not seem to filter?

Code: Select all

Sub Filter4Apple()
    Range("N11").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=14, _
      Criteria1:="=*Expired*", Operator:=xlAnd
End Sub
My headers are on 10. Why is this? I have 11 columns from D to N.
Best Regards,
Adam

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Filter rows containing specific text

Post by Don Wells »

adam wrote:The code does not seem to filter?
My headers are on 10. Why is this? I have 11 columns from D to N.
Try this:

Code: Select all

    Sub Filter4Apple()
        Range("D10:N10").AutoFilter
        Selection.AutoFilter Field:=11, _
          Criteria1:="=*Expired*", Operator:=xlAnd
    End Sub
Regards
Don

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

Re: Filter rows containing specific text

Post by adam »

Yeah it does help. Instead of using another button to unfilter, how could the line to remove the filter be used so that when the user first clicks the button if the sheet is not filtered, it gets filtered and when the user clicks the button again the sheet gets unfiltered?
Best Regards,
Adam

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

Re: Filter rows containing specific text

Post by adam »

By the way the when the filter is applied to the sheet the code causes debug message. The code works if filter is not applied to the sheet. How could it be made to work by having the filter applied?
Best Regards,
Adam

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Filter rows containing specific text

Post by Don Wells »

The following toggling code should suffice:

Code: Select all

    Sub Filter4Apple()
      If Not ActiveSheet.AutoFilterMode Then
        Range("D10:N10").AutoFilter
        Selection.AutoFilter Field:=11, _
          Criteria1:="=*Expired*", Operator:=xlAnd
      Else
        Range("D10:N10").AutoFilter
      End If
    End Sub
Regards
Don

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

Re: Filter rows containing specific text

Post by adam »

Thanks for the help Don. It works fine now and I do really appreciate your help.
Best Regards,
Adam