Advanced Filter with OR and Multiple Criteria per Column & Combo Box

rebeubeu

New Member
Joined
Feb 17, 2016
Messages
1
Hi guys,

I have been using this forum for quite a long time as a 'spectator'. This is my first post.

I have an advanced filter macro in place for my company. It allows us to match our prospective buyers to our signed content sellers by matching their produced content with the demographics/verticals.

Please refer below to see the VBA code that I am using and the filter rows/criteria.


RegionCountryPublisherWebsiteWebsites URLWebsite TierDemographicCategoryABC1Dec-2425 - 4950+Auto/MotoB2BBeautyBusiness/ Banking/ InsuranceCinema/ TV/ MusicCulture/ LifestyleEducationEntertainment/ BuzzFamily/ MotherhoodFashionFood/ BeverageGamingGreen DevelopmentHealthHobbies/ InterestsHome/ Deco/ Gardening/ DIYInfo/ 411LuxuryNews MediaPetsScienceSportTechnologyTravel/ Adventure

<colgroup><col><col><col><col><col><col><col><col span="5"><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col span="7"><col></colgroup><tbody>
</tbody>




<tbody>
</tbody>




' AdFilter Macro
' Filters Data
'
' Keyboard Shortcut: Option+Cmd+Shift+D
'
Sheet6.Range("B1").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Range("D7:AR8"), CopyToRange:=Range("D10:AR10"), Unique:= _
False
Range("O10").Select
End Sub

And then a small macro to clean the selection and the data retrieved. Both are assigned to buttons.

This has been working amazingly well but there are some challenges that I can't seem to solve. This are the challenges that I can't solve (I am still quite new to excel/vba so apologies in advance for the noob questions):



  • I can't fit more than one item per column, i.e., if I want to retrieve all the websites from Italy it's alright, but If I want to retrieve all of the websites from Italy and all of the websites from Spain, with doesn't retrieve any information. Also, is it possible to do it using a combo box selection as a filter.
  • When filtering, sometimes I want the doc to retrieve all the websites that match a category or another category (or both), i.e., all the websites that fit into tech and science. Is it possible to do that?

Thank you very much for your help, it is much appreciated. Don't hesitate to PM if you need any more information.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Modify your criteria range so it has as many data rows as you need for multiple items per column. In the case you mentioned above you should have CriteriaRange:=Range("D7:AR9") with 'Italy' and 'Spain' in the 'Country' column.
Values in the same column are treated as 'OR'
Values in the same row are in an 'AND' relationship

You can use data validation (drop down) in your criteria range.

This code moves the output down to accommodate a criteria range with a variable number of rows.

Code:
Option Explicit

Sub CopyFilteredDataToAnotherSheet()

    Dim wksData As Worksheet
    Dim wksResults As Worksheet
    Dim rngData As Range
    Dim rngCriteria As Range
    Dim rngOutput As Range
    
    Set wksData = Sheet6
    Set wksResults = ActiveSheet
    
    If wksResults.Name = wksData.Name Then
        MsgBox "Start code on the results worksheet"
        GoTo End_Sub
    End If
    
    Set rngData = wksData.Range("B1").CurrentRegion
    Set rngCriteria = wksResults.Range("D7").CurrentRegion
    
    'Output header will start 1 row below the last criteria row
    Set rngOutput = rngCriteria.Offset(rngCriteria.Rows.Count + 1)
    
    rngData.AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=rngCriteria, CopyToRange:=rngOutput, Unique:=False
    
End_Sub:
       
    Set rngOutput = Nothing
    Set rngCriteria = Nothing
    Set rngData = Nothing
    Set wksResults = Nothing
    Set wksData = Nothing
    
    
'With a CriteriaRange of A1:F12 the following would return all rows where
'  column A contained XX and B:F contained any value
'   A   B   C   D   E   F
'1  XX

'With a CriteriaRange of A1:F2 the following would return all rows where
'  column A contained XX and B:F contained any value OR
'  column A contained YY and B:F contained any value
'   A   B   C   D   E   F
'1  XX
'2  YY
'3
'If the CriteriaRange was A1:F3, the above would return all rows where
'  column A contained XX and B:F contained any value OR
'  column A contained YY and B:F contained any value OR
'  columns A:F contained any value

'With a CriteriaRange of A1:F4 the following would return all rows where
'  (column A contained XX and B:F contained any value OR
'  column A contained YY and B:F contained any value) OR
'  column D contained AA OR
'  column F contained BB
'   A   B   C   D   E   F
'1  XX
'2  YY
'3              AA
'4                      BB

'With a CriteriaRange of A1:F4 the following would return all rows where
'  (column A contained XX and B:F contained any value OR
'  column A contained YY and B:F contained any value) OR
'  (column B contained AA and C:F contained any value OR
'  column B contained BB and C:F contained any value) OR
'   A   B   C   D   E   F
'1  XX
'2  YY
'3      AA
'4      BB

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,635
Messages
6,120,660
Members
448,975
Latest member
sweeberry

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