You are here:

Excel/Filtering from a macro using external cell



   It's been a while since you helped me last, but you are my go-to guy for macros.

   In this case, I have a table with a filter applied.  I need to create a line (or two!) in a macro that will apply a numerical filter to a certain column, and will use the value in a cell (which is outside of the table) to show only rows where the value in the column equals the value in the cell.

   I hope you can get back to me quickly (as usual).  I'm hoping the answer isn't very complicated.

   Thanks as always,



I assume there is only one Table on the sheet so I just use the index number of 1 in the ListObjects collection to set a reference to the table (listobject)

Field:=1  applies the filter to the first column in the table.  Change to reflect your table.

Criteria1:=Range("L2").Value     this is the cell outside the table that has the numerical value to use as the filter value.

Sub FilterTable()
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects(1)
tbl.Range.AutoFilter Field:=1, Criteria1:=Range("L2").Value

End Sub

this actually removes the filter dropdown arrows.
Sub removefilter()
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects(1)
tbl.ShowAutoFilter = False
End Sub

If you just wanted to show all rows but leave the filter arrow showing then

Sub UnFilterTable()
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects(1)
tbl.Range.AutoFilter Field:=1       ' just remove any criteria for the filtered column

End Sub

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 All rights reserved.

[an error occurred while processing this directive]