• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Please help with filtering macro

I have a macro which I want to filter items. I need some assistance please.

Currently the macro works, but only when it filters one value. How do I go about having it filter multiple values?

I tried:
 rg.AutoFilter Field:=iCol, Criteria1:="TEST1"&"TEST2"

Open in new window

(but this didnt work).

Full code below:
Sub FilterSpecialType(ws As Worksheet)
'start filter code
Dim rg As Range, rgCopy As Range
With ws
    Set rg = .Range("J1").CurrentRegion
    Set rgCopy = rg.Offset(1, 0).Resize(rg.Rows.Count - 1, rg.Columns.Count)
    iCol = .Range("J:J").Column - rg.Column + 1
    rg.AutoFilter Field:=iCol, Criteria1:="TEST1"
    With Worksheets.Add(After:=Worksheets(Worksheets.Count))       'Destination worksheet
        .Cells(1, 1).PasteSpecial xlPasteValues
        Cells(1, 1).Select
        '.Name = "Filtered from " & ws.Name
        .Name = "Filtered Electronic"
    End With
End With
End Sub

Open in new window

  • 3
1 Solution
Have you considered creating a composite field and filtering on that?  For example, if I had a worksheet with FirstName in column A, LastName in column B, and Location in column C, I could create a composite of those values (i.e. value in column A + value in column B + value in column C) and put it in column D, then filter on column D.

A           B             C               D
John      Smith     London    JohnSmithLondon
Bob       Hughes  Delhi        BobHughesDelhi
Ted        Turner   Paris         TedTurnerParis

If you deemed the composite column too unslightly, simply hide the column.

Hope this helps!
eastsidemarketAuthor Commented:
Thanks for your reply.

To be clear, I have many values in this column. I really only want to filter out specific criteria.

So the composite above wouldn't really help.

I'd need to be able to automate just as I would checking off the items once its filtered.

I figure out how to get 1 item to filter in the code, but can't figure out how to get multiple items checked.

please advise!
eastsidemarketAuthor Commented:
i found my own solutions. thanks!

 rg.AutoFilter Field:=iCol, Criteria1:=Array( _
    "item1", "item2", "item3", "item4"), Operator:=xlFilterValues

Open in new window

eastsidemarketAuthor Commented:
found my own solution above.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now