Vba criteria multiple sheets

Klyde73

New Member
Joined
Jul 6, 2018
Messages
8
Hello,
I have this kode that works in Sheet1 but i need it to filter from the same inputbox criteria in Sheet2 as well.
My kode is:
Sub Filter_4()
x = InputBox("write the first 4 numbers")
x = Val(x & "000")
y = x + 999

Sheets("Sheet1").Range("$C:$C").AutoFilter Field:=1, Criteria1:=">=" & x, Operator:=xlAnd, Criteria2:="<=" & y

End Sub

Hope anyone knows how.
Regards
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Edit:
Actually i have 6 Sheets called Sheet1, Sheet2 etc.
I need it to filter in all these Sheets Range c:c with only one inputbox value.
 
Upvote 0
Hello Klyde73,

You could try this:-
Code:
Sub Filter_4()

        Dim ws As Worksheet
        x = InputBox("write the first 4 numbers")
        x = Val(x & "000")
        y = x + 999

For Each ws In Worksheets
With ws.[A1].CurrentRegion
       .AutoFilter 3, ">=" & x, xlAnd, "<=" & y
       'Do something here
       .AutoFilter
       End With
Next ws

End Sub


I've assumed the data in each sheet starts in column A, row2 with headings in row1.
I also assume that you would want to do something with the filtered data after filtering.

I hope that this helps.

Cheerio,
vcoolio.
 
Last edited:
Upvote 0
Hello again Klyde73,

Just an afterthought incase you just want to filter each sheet individually and nothing more.

With your code in post #1 , just change Sheets("Sheet1") to ActiveSheet.
The code will then operate on each individual sheet as you require, whereas my code in post #3 will filter all sheets at once.

Cheerio,
vcoolio.
 
Last edited:
Upvote 0
Hello vcoolio,

Thanks for helping out, but another issue is that i have 2 more Sheets that i have to leave unfiltered.
Sheets 7 & 8.
But you really have helped me getting gloser to the solution.
 
Upvote 0
Hello Klyde,

With the code in post #3 , change it to:-

Code:
Sub Filter_4()

          Dim ws As Worksheet
          x = InputBox("write the first 4 numbers")
          x = Val(x & "000")
          y = x + 999

For Each ws In Worksheets
[COLOR=#ff0000]If ws.Name <> "Sheet7" And ws.Name <> "Sheet8" Then[/COLOR]

With ws.[A1].CurrentRegion
             .AutoFilter 3, ">=" & x, xlAnd, "<=" & y
             'Do something here
              .AutoFilter
              End With
        End If
Next ws

End Sub

This will exclude Sheets 7 and 8 but will filter the rest.

But again, if you make the change as noted in post #4 , you will be able to filter each sheet individually so whether or not you filter sheets 7 and 8 is totally up to you.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
You're welcome Klyde. I'm glad that that I was able to help.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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