You are here:

Excel/Filtering data using a macro


Hello Bob,

I hope you can help me with an Excel 2010 macro question.

I have a spreadsheet containing data and I have applied an autofilter to one of the columns.  By recording a macro I have found how to use a macro to set the filter criteria thus

Sub FilterMacro()
   ActiveSheet.Range("$BD$2:$BD$77").AutoFilter Field:=1, Criteria1:=Array("grp 10", "grp 11", "grp 12"), Operator:=xlFilterValues
End Sub

My problem is that I would like the criteria to be a variable controlled by the macro rather than hard coded.   In an attempt to do this I created a formula in a cell in the spreadsheet which depending on various factors results in the criteria I want to filter.   This cell I named FilterString.

To reproduce the example above the cell FilterString would result in

"grp 10", "grp 11", "grp 12"

Then I modified the macro thus

Sub FilterMacro2()
Filtering = Range("FilterString").Value
   ActiveSheet.Range("$BD$2:$BD$77").AutoFilter Field:=1, Criteria1:=Array(Filtering), Operator:=xlFilterValues
End Sub

but this does not work.   It filters the data but it is as if the Criteria is "none".

Can you advise how to achieve my desired result?

Thanks for your help.


Instead of putting the variables in one cell, put them in a column in separate cells, like in column H (Change as necessary), then you can use this:
Sub FilterMacro2()
   Dim arr() As Variant
   n = Application.CountA(Columns(8))   'H
   ReDim arr(n)
   For i = 1 To n
       arr(i) = Cells(i, 8).Value       'H
   ActiveSheet.Range("$BD$2:$BD$77").AutoFilter Field:=1, _
         Criteria1:=Array(arr), Operator:=xlFilterValues
End Sub

It didn't work as you tried it because the contents of "filtering" was not ("grp 10", "grp 11", "grp 12"), bbut was (""grp 10", "grp 11", "grp 12"") -- note the surrounding quotes.

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


Bob Umlas


I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at


Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Excellence, The Expert, Microsoft

BA in math, Hofstra University, 1965

Awards and Honors
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 All rights reserved.