vba help needed, multiple criteria based on cell values to filter single field in range

bentez2003

New Member
Joined
Feb 26, 2013
Messages
43
Hi,

I have a list of countries in cells a2:a12 on sheet1 (some are duplicates) that i would like to use as my search criteria.
I have a range of data on sheet2 b2:g200, column B contains available countries.

How can i filter B by my multiple criteria cells?

the below is what i started working with but i can't get my different criteria to group together when filtered. the below leaves me with an empty filter.

Does it matter that some of the cells on sheet1 will be duplicates?

Thanks


Code:
Sub DoFilter()


Dim rCrit1 As Range, rCrit2 As Range, rRng1 As Range, rRng2 As Range


With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With


Set rCrit1 = Worksheets("Summary").Range("A2")
Set rCrit2 = Worksheets("Summary").Range("A3")


Set rRng1 = Worksheets("Funds").Range("B2:G208")
Set rRng2 = Worksheets("Funds").Range("B3:G208")


With rRng1


    .AutoFilter field:=1, Criteria1:=rCrit1.Value, Operator:=xlOr
    .AutoFilter field:=1, Criteria2:=rCrit2.Value
    rRng2.SpecialCells(xlCellTypeVisible).EntireRow.Copy
    .AutoFilter
End With


Application.EnableEvents = True


End Sub

 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Would using an advance filter be the way to go?

I have tried using the tool on the data tab however when i set the criteria it does filter my range selection.

Can anyone tell me what I am doing wrong?

Sorry if bumps arent allowed

Thanks
 
Upvote 0
This was the solution i came up with if anyone in the future has a similar problem


Code:
Sub FilterOn()


Dim rng As Range


Set rng = Worksheets("Funds").Range("B2:G208")
FilterField = WorksheetFunction.Match("affected country", rng.Rows(1), 0)


'Turn on filter if not already turned on
  If ActiveSheet.AutoFilterMode = False Then rng.AutoFilter
'Filter Specific Countries
  rng.AutoFilter Field:=FilterField, Criteria1:=Array( _
    [Summary!A2], [Summary!A3], [Summary!A4], [Summary!A5], [Summary!A6], [Summary!A7], _
    [Summary!A8], [Summary!A9], [Summary!A10], [Summary!A11], [Summary!A12]), Operator:=xlFilterValues
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,620
Messages
6,179,927
Members
452,949
Latest member
beartooth91

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
Back
Top