VBA Filter based on multiple Search Inputs

Bluewool

New Member
Joined
Aug 3, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hey All,

Im currently looking to Filter data in column H based on the user inputted data, currently I have the bellow code which works for 1 or 2 inputs, but I needed it for more for example 1-15 inputs at time.

Sub FilterValues()

Dim Area As String
Dim Data As Variant

Area = InputBox("Enter Selection - Seperated by Comma")
If InStr(1, Area, ",") > 0 Then
Data = Split(Area, ",")
ActiveSheet.Range("$H$1:$H$30000").Select
Selection.AutoFilter
ActiveSheet.Range("$H$1:$H$30000").AutoFilter Field:=1, Criteria1:=Trim(Data(0)), Operator:=xlOr, Criteria2:=Trim(Data(1))
Else
ActiveSheet.Range("$H$1:$H$30000").AutoFilter Field:=1, Criteria1:=Area
End If
End Sub


any help would be apricated.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
When posting code, please indent your code and use the forum's code tags to preserve that indentation. It will help you get better/faster responses as code like that is much easier to read & debug.

Give this a try with a copy of your workbook. I added a bit so that if nothing is entered in the Input Box, no filter is applied.
If you might be running this code multiple times on the same data, we probably need to add some more to ensure that any existing filter in column H (& other columns?) is removed before applying the new one. Post back if you need that.

VBA Code:
Sub FilterValues_v2()
  Dim Area As String
 
  Area = InputBox("Enter Selection - Seperated by Comma")
  If Len(Area) > 0 Then ActiveSheet.Range("$H$1:$H$30000").AutoFilter Field:=1, Criteria1:=Split(Area, ","), Operator:=xlFilterValues
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,751
Messages
6,126,669
Members
449,326
Latest member
asp123

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