filter by code

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

filter by code

Post by bknight »

I'm looking for a bit of code to filer by a worksheet.

Code: Select all

dim I as Long
i=2
Do
If Cells(I,1) = "ABC) or Cells(i,1) = "DEF" or Cells(I, 1) = "GHI" then
At this point I don't know how to filter a sheet by all of the strings, there will be about 20 total strings(3 of which are coded).
The workbook is named Chart.csv and the Sheet is named Sheet2.

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

Re: filter by code

Post by HansV »

A bit shorter:

Code: Select all

        Select Case Cells(I, 1).Value
            Case "ABC", "DEF", "GHI", ..., "XYZ"
                ' Do something
        End Select
or enter the 20 values in a range, say Z1:Z20, and use

Code: Select all

        If Not Range("Z1:Z20").Find(What:=Cells(I, 1).Value, LookAt:=xlWhole) Is Nothing Then
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: filter by code

Post by bknight »

Ok, I can enter the strings into Z1:Z20 but how to filter the sheet with those strings? The strings are in column 1 currently, but if I add them to Z then they would be in column 26. How do I code those strings to filter on column 26?

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

Re: filter by code

Post by HansV »

I meant: enter the strings ABC, DEF etc. in Z1:Z26.

What exactly is the purpose of your code?
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: filter by code

Post by bknight »

Purpose is to limit the number of displayed rows from 600 down to 20.

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

Re: filter by code

Post by HansV »

Move the strings to Z2:Z27.
Enter the same heading in Z1 as in A1

Code: Select all

Range("A:A").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("Z1:Z27")
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: filter by code

Post by bknight »

That worked almost 100%, there were 4 rows displayed not in the target range, but I can accept that.
Thanks as always.