Macro for applying filter to all sheets.

strongman86

Board Regular
Joined
Feb 15, 2017
Messages
115
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I'm looking for a macro that applies the Label filter -> does not contain APPLE to Cell A2 in all sheets in workbook every time the workbook is opened. Thank you.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try this

Place code in ThisWorbook Module
- does not work if placed in a standard module

Code:
Private Sub Workbook_Open()
    AppleCore
End Sub

Private Sub AppleCore()
    Const X = "<>*APPLE*"
    Dim W As Worksheet, R As Range
    On Error Resume Next
    
    For Each W In ThisWorkbook.Worksheets
        Set R = W.Cells(2, 1).Resize(W.Rows.Count - 1, W.Columns.Count)
        W.ShowAllData
        R.AutoFilter Field:=1, Criteria1:=X, Operator:=xlAnd
    Next
End Sub
 
Upvote 0
Hi Yongle,

This doesn't seem to work. Forgot to mention all sheets are pivottables if this makes any difference.
 
Upvote 0
Forgot to mention all sheets are pivottables
Yes - that would make a difference

This is waht I was using to test :confused:
Excel 2016 (Windows) 32 bit
A
B
C
D
1
2
Header1
Header2
Header3
3
The apple is red
07/08/2019​
104​
4
TheOrange is orange
02/08/2019​
420​
5
The pear is green
06/08/2019​
252​
6
The apple is red
11/08/2019​
883​
7
TheOrange is orange
09/08/2019​
558​
8
The pear is green
08/08/2019​
101​
9
The apple is red
04/08/2019​
153​
10
TheOrange is orange
11/08/2019​
484​
11
The pear is green
03/08/2019​
401​
12
The apple is red
09/08/2019​
568​
13
14
Sheet: Sheet1
 
Upvote 0
You could try this
Code:
Sub Possibly()
    Dim ws As Worksheet
    On Error Resume Next
    For Each ws In ThisWorkbook.Worksheets
        ws.PivotTables(1).PivotFields(1).ClearAllFilters
        ws.PivotTables(1).PivotFields(1).PivotFilters.Add2 Type:=xlCaptionContains, Value1:="APPLE"
    Next
End Sub

If above does not work
- try recording a macro when manually amending the filter
- modify the recorded macro to give you what you require
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,105
Members
449,066
Latest member
Andyg666

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