+ Reply to Thread
Results 1 to 5 of 5

Macro for Autofilter that filters on a column based on the value of a cell

  1. #1
    Registered User
    Join Date
    11-04-2014
    Location
    Los Angeles
    MS-Off Ver
    2007
    Posts
    14

    Macro for Autofilter that filters on a column based on the value of a cell

    I am attempting to filter a column depending on a value in another cell. The value is used to identify the column number in a range. So if the value is 4, then I want to filter column 4.

    This is what my table in excel looks like:
    xtable image.jpg


    In this screen shot, the value in Q3 = 4, so I want column V (which is the 4th column in my range as the range is from S to AC) to filter for the 1s.

    So far I have this code which creates the filters and runs the filter. “Subject_PropertyTypeTitles” is the range name of the title row where the filters are created:

    Application.Goto Reference:="Subject_PropertyTypeTitles"
    Selection.AutoFilter
    ActiveSheet.Range("$R$3:$AC$104").AutoFilter Field:=1, Criteria1:="Entry"
    ActiveSheet.Range("$R$3:$AC$104").AutoFilter Field:=4, Criteria1:="1"


    However, where it says “Autofilter Field:=4”, I want the 4 to be whatever number is in Q3.

    Can you please help?

    Thanks,

    Jen

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Macro for Autofilter that filters on a column based on the value of a cell

    Change the 4 to "Cells(3,22).value" (without the speech marks)

  3. #3
    Registered User
    Join Date
    11-04-2014
    Location
    Los Angeles
    MS-Off Ver
    2007
    Posts
    14

    Re: Macro for Autofilter that filters on a column based on the value of a cell

    I changed the 4 to cells(3.22).value, but it gave an error. Here is the code now:
    Application.Goto Reference:="Subject_PropertyTypeTitles"
    Selection.AutoFilter
    ActiveSheet.Range("$R$3:$AC$104").AutoFilter Field:=1, Criteria1:="Entry"
    ActiveSheet.Range("$R$3:$AC$104").AutoFilter Field:=(3,22).value, Criteria1:="1"

    It says, "compile error, syntax error".
    Also, can you please explain what 3,22 represents? I am new to VBA code.

    Thanks!

  4. #4
    Registered User
    Join Date
    11-04-2014
    Location
    Los Angeles
    MS-Off Ver
    2007
    Posts
    14

    Re: Macro for Autofilter that filters on a column based on the value of a cell

    Another option is that I can write an if then statement for every scenario. For instance, If Q3=1, then ActiveSheet.Range("$R$3:$AC$104").AutoFilter Field:=1, Criteria1:="1", else if Q3=2, then ActiveSheet.Range("$R$3:$AC$104").AutoFilter Field:=2, Criteria1:="1"

    However, I do not know how to write if then statements in VBA. Please help me come up with a solution!

    Jen

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro for Autofilter that filters on a column based on the value of a cell

    Hi Jen

    Change
    Please Login or Register  to view this content.
    To
    Please Login or Register  to view this content.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy top cell post autofilter and re-apply autofilter based on cell value
    By gpato in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-20-2014, 09:07 PM
  2. Autofilter Column of Data Based on Cell Input
    By juliettelam in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-31-2014, 11:30 AM
  3. [SOLVED] Display results from a list based on multiple column filters.
    By Snoddas in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-12-2014, 12:41 AM
  4. How do I update filters when using Autofilter?
    By SunGod_69 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-25-2006, 02:00 AM
  5. Replies: 1
    Last Post: 01-23-2006, 02:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1