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
calyx_terenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

calyx_terenAuthor Commented:
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
Martin LissOlder than dirtCommented:
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.
Martin LissOlder than dirtCommented:
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
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

calyx_terenAuthor Commented:
I tried it, but it didn't work. Attaching the spreadsheet. The Marketing button is not working.
Dummy-Data-Marketing-Toolbox-MACRO.xlsm
Martin LissOlder than dirtCommented:
You have a table so...

ActiveSheet.ListObjects("Table2").Range.AutoFilter

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
calyx_terenAuthor Commented:
That works! Now, is there a way to keep the dropdown filtering capability? It disappears when I run the macro.
Martin LissOlder than dirtCommented:
Starting to make dinner. Will get back to you.
calyx_terenAuthor Commented:
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!
Martin LissOlder than dirtCommented:
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
calyx_terenAuthor Commented:
Thanks, Martin. I'll take a look.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.