Hide Rows based on user input

gd6noob

Board Regular
Joined
Oct 20, 2017
Messages
170
Office Version
  1. 2016
Platform
  1. Windows
I have a code below that allows user input to select which sheet and criteria to hide.
Is there a way to allow multiple sheets to filter at once.
i.e. I have cells B1,B2,B3,B4 and B5 with sheets names, if I have B2 and B4 as my active cell (selected cells) and run the macro with criteria, it will filter those sheets?

VBA Code:
Sub filter()

    Dim noRows As Integer
    Dim myRange As String
    Dim searchRange As String
    Dim criteria_1 As String
    Dim mysheet As String
    
    mysheet = Application.InputBox("Enter Sheet Name")
    
    noRows = Worksheets(mysheet).UsedRange.Rows.Count
    noRows_cor = noRows + 3
    myRange = "A1:A" & noRows_cor
    criteria_1 = Application.InputBox("Enter your Criteria:")
         
    Worksheets(mysheet).Range(myRange).AutoFilter Field:=1, Criteria1:="<>" & criteria_1
          
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
if I have B2 and B4 as my active cell (selected cells) and run the macro

How about:

VBA Code:
Sub filter()
  Dim noRows As Integer, c As Range
  Dim myRange As String, criteria_1 As String, mysheet As String
  
  criteria_1 = Application.InputBox("Enter your Criteria:")
  For Each c In Selection
    mysheet = c.Value
    noRows = Worksheets(mysheet).UsedRange.Rows.Count
    myRange = "A1:A" & noRows + 3
    Worksheets(mysheet).Range(myRange).AutoFilter Field:=1, Criteria1:="<>" & criteria_1
  Next
End Sub
 
Upvote 0
How about:

VBA Code:
Sub filter()
  Dim noRows As Integer, c As Range
  Dim myRange As String, criteria_1 As String, mysheet As String
 
  criteria_1 = Application.InputBox("Enter your Criteria:")
  For Each c In Selection
    mysheet = c.Value
    noRows = Worksheets(mysheet).UsedRange.Rows.Count
    myRange = "A1:A" & noRows + 3
    Worksheets(mysheet).Range(myRange).AutoFilter Field:=1, Criteria1:="<>" & criteria_1
  Next
End Sub
Thank you very much, this works well.
 
Upvote 0
Ohh just realized that I can only have 1 filter at a time, anyway to keep adding more filters?
 
Upvote 0
Ohh just realized that I can only have 1 filter at a time, anyway to keep adding more filters?
What do you mean by adding more filters?
What you asked for is to filter with a criteria taking the name of the sheets of the selected cells (B1: B5)
 
Upvote 0
What do you mean by adding more filters?
What you asked for is to filter with a criteria taking the name of the sheets of the selected cells (B1: B5)
Sorry for not being clear. When adding the first criteria, it works but when trying to add another one, the first criteria is cleared.
 
Upvote 0
What is the second criteria?
How many criteria are you going to add?
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,789
Members
449,126
Latest member
Greeshma Ravi

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