Custom filtering

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to create a custom filter but am stuck on the logic.

Assume I only have two criteria (the real version has many more): Fruit and Location.

Cells A1 and B1 contain data validation. The data starts from row 10.

If the user selects a particular Fruit and Location, I only want rows that satisfy that combination to be shown - that I can do.

Code:
    Dim Counter As Long
    
    For Counter = 10 To 15
    
        If Sheet1.Cells(Counter, 1).Value = Sheet1.Cells(2, 1).Value And _
           Sheet1.Cells(Counter, 2).Value = Sheet1.Cells(2, 2).Value Then Sheet1.Rows(Counter).Hidden = False
            
    Next Counter

What I find tricky is the data validation also contains an "All" value. So if the criteria for Fruit was "All" and Location was "UK", I want all rows where Location is "UK" to be shown. The Fruit criteria is relaxed.

How would the code for this look like? As I said, if it's only two criteria, it's relatively straightforward but in reality, I have many more criteria and the code becomes very long.

Thanks



1614004172806.png
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You could do something like this

Code:
If (Sheet1.Cells(Counter, 1).Value = Sheet1.Cells(2, 1).Value Or Sheet1.Cells(2, 1).Value = "All") And _
           (Sheet1.Cells(Counter, 2).Value = Sheet1.Cells(2, 2).Value Or Sheet1.Cells(2, 2).Value = "All") Then Sheet1.Rows(Counter).Hidden = False
 
Upvote 0
Solution
You could do something like this

Code:
If (Sheet1.Cells(Counter, 1).Value = Sheet1.Cells(2, 1).Value Or Sheet1.Cells(2, 1).Value = "All") And _
           (Sheet1.Cells(Counter, 2).Value = Sheet1.Cells(2, 2).Value Or Sheet1.Cells(2, 2).Value = "All") Then Sheet1.Rows(Counter).Hidden = False
Thanks for your speedy reply.

My only worry is given I have a lot more criteria (10), the combinations becomes very long.

Still, as long as it works ..... :)
 
Upvote 0
You'd probably be better off actually using filters.
 
Upvote 0
I mean that you could achieve the result you want using actual filters on the range in question.
 
Upvote 0

Forum statistics

Threads
1,214,426
Messages
6,119,411
Members
448,894
Latest member
spenstar

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