Tweak code - filter multiple sheets

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,066
Office Version
  1. 365
Platform
  1. Windows
I have this code which works, but I need to finesse it;

Code:
Sub filtersheets()
Dim xWs As Worksheet
On Error Resume Next
For Each xWs In Worksheets
xWs.Range("b4").AutoFilter 2, Sheets("total").Range("C1")
Next
End Sub

What I want to change;

- if C1 contains "ALL" it unfilters
- any change to cell C1 triggers the event, rather than having to run a macro
- code runs on every sheet except "TOTAL"

TIA
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi,
Untested but try this update to your code

Standard Module
Code:
Sub filtersheets(ByVal sh As Object)
    Dim xWs As Worksheet
    Dim FilterCriteria As String
    
    FilterCriteria = sh.Range("C1").Value
    
    On Error Resume Next
    For Each xWs In Worksheets
        If FilterCriteria = "All" Then
            xWs.Range("b4").AutoFilter
        Else
        If xWs.Name <> sh.Name Then xWs.Range("b4").AutoFilter 2, FilterCriteria
        End If
    Next
    On Error GoTo 0
End Sub

Your total sheets code page

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 filtersheets Me
End Sub

Solution assumes change of values in C1 are other than by formula

Hope Helpful

Dave
 
Last edited:
Upvote 0
Nearly perfect;

Entering "ALL" tries to filter by that text, I need it to unfilter/show all if "ALL" is entered.

Thanks :-)
 
Last edited:
Upvote 0
Most welcome

Dave

Sorry, nearly perfect;

Entering "ALL" tries to filter by that text, I need it to unfilter/show all if "ALL" is entered.

Thanks :-)
 
Last edited:
Upvote 0
Its ok I've just realised the word "ALL" is case sensitive

Thanks again!
 
Upvote 0
Try this update

Code:
Sub filtersheets(ByVal sh As Object)
    Dim xWs As Worksheet
    Dim FilterCriteria As String
    
    FilterCriteria = sh.Range("C1").Value
    If Len(FilterCriteria) = 0 Then Exit Sub
    
    On Error Resume Next
    For Each xWs In Worksheets
        With xWs
            If UCase(FilterCriteria) = "ALL" Then
             If .FilterMode Then .ShowAllData
            Else
                If .Name <> sh.Name Then .Range("b4").AutoFilter 2, FilterCriteria
            End If
        End With
    Next
    On Error GoTo 0
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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