Trying to filter data incrementally based on user selection

rizzo93

Active Member
Joined
Jan 22, 2015
Messages
299
Office Version
  1. 365
I'm trying to filter data based on a user's selection. The user may select more than one criteria.

I have a column called "PRODUCTS" containing a number of values (e.g., PRODUCT 1, PRODUCT 2, etc.).

I have 11 checkboxes (on the sheet itself, not a userform), each representing a product in that column. When the user checks the box for "PRODUCT 1", the data should filter accordingly.

But if they then select "PRODUCT 4" and then "PRODUCT 6", then the data should be filtered on all 3 selections.

I recorded a macro to have something to work with:

Code:
ActiveSheet.Range("$B$9:$N$193").AutoFilter Field:=1, Criteria1:=Array("PRODUCT 1"), Operator:=xlFilterValues

I then copied it and changed the criteria:

Code:
Sub filteronproduct()[INDENT]ActiveSheet.Range("$B$9:$N$193").AutoFilter Field:=1, Criteria1:=Array("PRODUCT 1"), Operator:=xlFilterValues[/INDENT]
[INDENT]ActiveSheet.Range("$B$9:$N$193").AutoFilter Field:=1, Criteria1:=Array("PRODUCT 2"), Operator:=xlFilterValues
ActiveSheet.Range("$B$9:$N$193").AutoFilter Field:=1, Criteria1:=Array("PRODUCT 3"), Operator:=xlFilterValues
ActiveSheet.Range("$B$9:$N$193").AutoFilter Field:=1, Criteria1:=Array("PRODUCT 4"), Operator:=xlFilterValues
[/INDENT]
End Sub

But whenever I execute that code, it only filters on the last product, which in the example above is "PRODUCT 4".

I think I need some sort of loop, but I'm still getting a grasp on that.

Any suggestions would be much appreciated.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I'm a total novice at VBA, so I'm having a real stab in the dark here, but my first thought would be the "Criteria1" part of the code. Do the other lines need to have incremental numbers? i.e. Criteria2, Criteria3 and Criteria4?

I'll be interested to know the answer for my own learnings!
 
Upvote 0
Hi NiMip,

Thanks for chiming in. In reality, the "product" values are not going to be numbered.

As for the criteria, and based on my limited knowledge of VBA, those will definitely be numbered. My plan is to list out all the values possible and have the checkboxes link to the corresponding values in that list like so:

AB
PRODUCT 1False
PRODUCT 2True
PRODUCT 3True
PRODUCT 4False
PRODUCT 5False
and so on

<TBODY>
</TBODY>

True
means the checkbox has been checked and therefore should add to the filter.
 
Upvote 0
After much trial and error, I realized that what I was trying to do was too complicated to account for all the combinations of variables being put together.


Instead, I decided to use a helper column for each condition. If the condition was met, it would be TRUE, if not, FALSE.


Filtering on just TRUE makes things a lot easier.
 
Upvote 0
Ah understood. I've recently been working on a new cost estimation tool for my workplace. I've had some great ideas on how to improve usability that turn out to be much more complicated to implement than I expected. Sometimes an extra click or two isn't so bad haha.
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,974
Members
448,934
Latest member
audette89

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