VBA code - filter: show all data and then remove filter

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
871
Office Version
  1. 365
Platform
  1. Windows
I have a code that shows all data on a filter but how can this code be modified to show all data AND remove the filter?

Code:
If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
  ActiveSheet.ShowAllData

I have to use this multiple times throughout the code.

I have this code but can only use it once in my macro or I get an error:

Code:
        Dim lo As ListObject  
  For Each lo In ActiveSheet.ListObjects
  
    lo.AutoFilter.ShowAllData
      Next lo

Still have a lot to learn about VBA

Thank you

Carla
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
How about

Code:
Sub Test2()
  Dim lo As ListObject
  For Each lo In ActiveSheet.ListObjects
    If lo.ShowAutoFilter Then
      lo.Range.AutoFilter
    End If
  Next lo
End Sub
 
Upvote 0
Could this be used multiple times in one string of code for different sheets?
 
Upvote 0
How about

Code:
Sub Test2()
  Dim lo As ListObject, sh As Worksheet
  For Each sh In Sheets
    For Each lo In sh.ListObjects
      If lo.ShowAutoFilter Then
        lo.Range.AutoFilter
      End If
    Next lo
  Next
End Sub
 
Upvote 0
I will have to use the code multiple times for different sheets. If I use the above code in its entirety more than once I get the following error:

Compile error: Duplicate declaration in current scope.

but if I remove the first line for the second entry it seems to work.

Is this correct?

Code:
  Dim lo As ListObject, sh As Worksheet  For Each sh In Sheets
    For Each lo In sh.ListObjects
      If lo.ShowAutoFilter Then
        lo.Range.AutoFilter
      End If
    Next lo
  Next

'More macro code


  For Each sh In Sheets
    For Each lo In sh.ListObjects
      If lo.ShowAutoFilter Then
        lo.Range.AutoFilter
      End If
    Next lo
  Next

'More Macro code
 
Upvote 0
Actually my original code works as well if I do this:

Code:
        Dim lo As ListObject
 
  For Each lo In ActiveSheet.ListObjects
 
    lo.AutoFilter.ShowAllData
      Next lo

'Macro code

 
  For Each lo In ActiveSheet.ListObjects
 
    lo.AutoFilter.ShowAllData
      Next lo

'More Macro code

End Sub

If you see any problems that may occur with doing it this way please let me know, otherwise I will roll with this. Thank you for all of your help!
 
Last edited:
Upvote 0
Commentary:

I will have to use the code multiple times for different sheets. If I use the above code in its entirety more than once I get the following error:

Compile error: Duplicate declaration in current scope.

but if I remove the first line for the second entry it seems to work.

Is this correct?

Code:
[COLOR=#ff0000]Dim lo As ListObject, sh As Worksheet[/COLOR]  '[COLOR=#008000]This line should only be once in the entire code.[/COLOR]
For Each sh In Sheets
    For Each lo In sh.ListObjects
      If lo.ShowAutoFilter Then
        lo.Range.AutoFilter
      End If
    Next lo
  Next
'More macro code
  For Each sh In Sheets
    For Each lo In sh.ListObjects
      If lo.ShowAutoFilter Then
        lo.Range.AutoFilter
      End If
    Next lo
  Next

'More Macro code
 
Upvote 0
Actually my original code works as well if I do this:

Code:
        Dim lo As ListObject
   For Each lo In ActiveSheet.ListObjects
     lo.AutoFilter.ShowAllData
      Next lo
'Macro code
  For Each lo In ActiveSheet.ListObjects
   lo.AutoFilter.ShowAllData
      Next lo
'More Macro code
End Sub

If you see any problems that may occur with doing it this way please let me know, otherwise I will roll with this. Thank you for all of your help!

But this code only shows the data (first part of your request), if I understood correctly you also wanted to remove the filter (second part of your request).

I have a code that shows all data on a filter but how can this code be modified to show all data AND remove the filter?

For the second part of your request is the code that I put in post #4 .
But if your last code works for you, then it's fine..
 
Upvote 0

Forum statistics

Threads
1,213,513
Messages
6,114,072
Members
448,546
Latest member
KH Consulting

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