Calyx Teren
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") .AutoFilte r
End If
ActiveSheet.ShowAllData
End Sub
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
ActiveSheet.Range("A1:Q1")
End If
ActiveSheet.ShowAllData
End Sub
I actually deleted my first answer but activesheet.cells.autofilt er 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
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
ASKER
I tried it, but it didn't work. Attaching the spreadsheet. The Marketing button is not working.
Dummy-Data-Marketing-Toolbox-MACRO.xlsm
Dummy-Data-Marketing-Toolbox-MACRO.xlsm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
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!
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
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
ASKER
Thanks, Martin. I'll take a look.
ASKER
Sub GlobalMktg()
Sheets("Toolbox").Select
Cells.EntireColumn.Hidden = False
Cells.EntireRow.Hidden = False
ActiveSheet.ShowAllData
End Sub