Easy Question I'm sure. Filter Results on each page open

thiess22

New Member
Joined
Mar 25, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I'm thinking this is an easy one but I can't get it to work for me. I have one work sheet that unfilters and then filters results on a cell change. I have two other worksheets though where I want to accomplish the same thing for cells on their respective pages but instead of on a cell change I'd like it to run each time the worksheet is clicked on.

Here's the code I'm using for the page than works on cell change. That works great.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range

' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("b12")

If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then

' Display a message when one of the designated cells has been
' changed.
' first remove filter
ActiveSheet.Range("A1:AE3200").AutoFilter Field:=7
' then apply it again
ActiveSheet.Range("A1:AE3200").AutoFilter Field:=7, Criteria1:="<>0"

End If
End Sub


I'm sure this is simple I just can't seem to find the answer and am not great with this stuff yet.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
VBA Code:
Private Sub Worksheet_Activate()
'    Code that you want to execute
End Sub

Put that in the sheet code and it will execute every time that sheet is selected.
 
Upvote 0
I tried that but I kept getting errors. I even tried just putting it at the beginning of this formula and I couldn't get it to run. Shouldnt this work?

Private Sub Worksheet_Activate()

ActiveSheet.Range("A1:AE3200").AutoFilter Field:=7

ActiveSheet.Range("A1:AE3200").AutoFilter Field:=7, Criteria1:="<>0"

End Sub
 
Upvote 0
No, I have one sheet I want it to run on when that sheet is clicked on. To filter out B10:E3000 for any lines in field 1 with the word "Hide"

Then I have a second sheet that I want it run as well when that sheet is clicked on that filters out a to be determined range.

Workbook has several other sheets as well that I don't filtered at all.
 
Upvote 0
Ok. Give me a moment. In the mean time, delete the code from post #3 from the sheet module you placed it in.
 
Upvote 0
Ok. In the 'ThisWorkbook' module paste the following code:

VBA Code:
Private Sub Workbook_SheetActivate(ByVal SelectedSheet As Object)
'
    If SelectedSheet.Name = "SomeSheeetName" Then       ' <--- fix the sheetName
'       Put your code here
    End If
'
    If SelectedSheet.Name = "AnotherSheetName" Then     ' <--- fix the sheetName
'       Put your code here
    End If
End Sub

Then correct the sheetnames and insert the code that you want to run on each sheet when that particular sheetname is clicked on.
 
Upvote 0
Cant get it to work. Perhaps i'm making a dumb mistake when try to put that the code in for the filter?


Private Sub Workbook_SheetActivate(ByVal SelectedSheet As Object)

If SelectedSheet.Name = "JOB ESTIMATE" Then


ActiveSheet.Range("C11:E702").AutoFilter Field:=1

ActiveSheet.Range("C11:E702").AutoFilter Field:=1, Criteria1:="<>0"



End If

End Sub
 
Upvote 0
I tried filtering outlines with zero in that code vs "Hidden"
 
Upvote 0
What does 'can't get it to work' mean? It does nothing? It errors? Please be specific about what it does/does not do.

I know almost nothing about filters, so I won't be of much help there.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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