Filter showalldata avoiding using SELECT

L

Legacy 365210

Guest
Hi All

I have code that needs to turn off any filters on 3 sheets before doing some actions, mainly find-replace and copy-paste.

My issue is the code to unfilter only works on the activesheet, I have declared the sheets and tried working through the variable directly (as below) but this just ignores the code for the inactivesheets and runs it on the activesheet (whichever one of the 3 I have open in the backround).

Now obviously I am trying my hardest to avoid the SELECT or ACTIVATE cardinal sins here - so any advice on getting this to check for and unfilter the 3 x sheets without select? Code below

Code:
Dim Pre As Worksheet
Set Pre = Worksheets("Pre")
Dim Post As Worksheet
Set Post = Worksheets("Post")
Dim PrePostCombined As Worksheet
Set PrePostCombined = Worksheets("Pre_Post_Combined")


If (Pre.AutoFilterMode And Pre.FilterMode) Or Pre.FilterMode Then
  Pre.ShowAllData
End If
    
If (Post.AutoFilterMode And Post.FilterMode) Or Post.FilterMode Then
  Post.ShowAllData
End If

If (PrePostCombined.AutoFilterMode And PrePostCombined.FilterMode) Or PrePostCombined.FilterMode Then
  PrePostCombined.ShowAllData
End If
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
could you loop through the sheets?

Code:
Sub Button1_Click()
    Dim sh As Worksheet
    For Each sh In Sheets
        With sh
            If .AutoFilterMode = True Then .AutoFilterMode = False
        End With
    Next sh
End Sub
 
Upvote 0
If your data is set up as a table, try
Code:
Sub chk()

   Dim Ary As Variant
   Dim ShtName As Variant
   
   Ary = Array("Pre", "Post", "Pre_Post_Combined")
   
   For Each ShtName In Ary
      If Sheets(ShtName).ListObjects(1).AutoFilter.FilterMode Then
         Sheets(ShtName).ListObjects(1).AutoFilter.ShowAllData
      End If
   Next ShtName
End Sub
 
Upvote 0
Thank you Fluff that's a good shout will give it a go and see how it behaves
 
Upvote 0
Hi Dave, I would do the loop isn't for all sheets just a few, will try with an array as fluff suggested and see what happens :-)
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,565
Members
452,652
Latest member
eduedu

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