You are here:

Excel/Filtering data using a macro

Advertisement


Question
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.

Phill

Answer
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()
   Selection.AutoFilter
   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
   Next
   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.

HTH
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

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bob Umlas

Expertise

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 http://www.thumbtack.com/ny/new-york/excel-training/

Experience

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."

Publications
Excellence, The Expert, Microsoft

Education/Credentials
BA in math, Hofstra University, 1965

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

©2016 About.com. All rights reserved.