Link to home
Start Free TrialLog in
Avatar of Calyx Teren
Calyx TerenFlag for United States of America

asked on

How to Unfilter Rows with VBA

I have a macro that is supposed to unhide all columns and unfilter rows, but the unfilter portion doesn't work.

I would like the macro to unfilter the spreadsheet if a filter is applied and to not return an error if there is no filter applied. I read that AutoFilter needs to be turned on for ShowAllData to work so I attempted to do that. Still not working.


Sub GlobalMktg()
Sheets("Toolbox").Select
Cells.EntireColumn.Hidden = False
Cells.EntireRow.Hidden = False
    If ActiveSheet.AutoFilterMode = False Then
    ActiveSheet.Range("A1:Q1").AutoFilter
    End If
ActiveSheet.ShowAllData
End Sub
Avatar of Calyx Teren
Calyx Teren
Flag of United States of America image

ASKER

Thanks for the help, Martin, but what I'm really looking for is a fool-proof way for ActiveSheet.ShowAllData to work. It needs to work whether there is a filter applied already or not. Any ways to fool-proof this code?

Sub GlobalMktg()
 Sheets("Toolbox").Select
 Cells.EntireColumn.Hidden = False
 Cells.EntireRow.Hidden = False
 ActiveSheet.ShowAllData
 End Sub
Avatar of Martin Liss
I actually deleted my first answer but activesheet.cells.autofilter will show all the data and there's no error if there's no filtering.
I also just found this on the web but haven't tested it.

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

Open in new window

This seems to prevent ShowAllData from running when there is no actual filter applied but with AutoFilterMode turned on.

The second catch Or ActiveSheet.FilterMode should catch advanced filters
I tried it, but it didn't work. Attaching the spreadsheet. The Marketing button is not working.
Dummy-Data-Marketing-Toolbox-MACRO.xlsm
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That works! Now, is there a way to keep the dropdown filtering capability? It disappears when I run the macro.
Starting to make dinner. Will get back to you.
Figured it out. Just added this section to the macro:
Dim Lst As ListObject
Set Lst = ActiveSheet.ListObjects(1)
    Lst.ShowAutoFilter = True

Thanks for the solution!
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
Thanks, Martin. I'll take a look.