+ Reply to Thread
Results 1 to 8 of 8

Macro to filter date based on a selection

  1. #1
    Registered User
    Join Date
    10-03-2013
    Location
    california
    MS-Off Ver
    Excel 2007
    Posts
    34

    Macro to filter date based on a selection

    Hi,

    Please help me with a macro that will filter a data table based on the value of a cell with a date (mm/dd/yy)

    The filter area is on column "B:B" and the reference for the filter is on another sheet on cell "C5"

    Here's the code that I tried

    Please Login or Register  to view this content.
    I am having an error with this line:

    Please Login or Register  to view this content.
    What is the correct way of doing this?

    Thank you for your help

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to filter date based on a selection

    The argument name is Criteria1

    ActiveSheet.Range("B3:T99").AutoFilter Field:=1, Criteria1:=Sheets("Formulas and Tables").Range("C5")
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    10-03-2013
    Location
    california
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: Macro to filter date based on a selection

    Thanks for pointing out the cause of error Alphabug!

    I got it to start filtering however, it will not find the value i was hoping it to do.

    It will filter out everything where it should leave the ones with the correct date in it.. Please see the attached workbook.

    Thank you

    Sample.xlsm

  4. #4
    Registered User
    Join Date
    10-03-2013
    Location
    california
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: Macro to filter date based on a selection

    This is weird, I have tried to filter the other columns but they were working. However the filter field is in text e.g. "Boss" . Why is the autofilter not working on dates?

    your help is very much appreciated

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to filter date based on a selection

    Quote Originally Posted by moxman View Post
    Thanks for pointing out the cause of error Alphabug!

    I got it to start filtering however, it will not find the value i was hoping it to do.

    It will filter out everything where it should leave the ones with the correct date in it.. Please see the attached workbook.

    Thank you
    Your dates in column B are Date-Time values as seen in the formula bar. The Autofilter function filters on the actual values and not the displayed values. Since the column B dates also have time values, none of them match exactly the date-only value in C5.

    This will filter for all times on a given Date
    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 10-22-2013 at 09:07 PM.

  6. #6
    Registered User
    Join Date
    10-03-2013
    Location
    california
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: Macro to filter date based on a selection

    I got it working!

    Thank you to alphadog for reply to my thread.

    It seems for "dates" to work, i need to add another line to the code.
    with the "array" being the subcategory of the date. as it goes deeper the higher the value is.

    I also tried your code alphadog and it works! how you came up with that is totally genius

    Please Login or Register  to view this content.
    Last edited by moxman; 10-22-2013 at 09:14 PM.

  7. #7
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Macro to filter date based on a selection

    Hi moxman, I have a question for you. Why would you need a macro if you can use already built-in feature in Excel 2007? Simply right-click on a cell and select Filter, and Filter by selected cell's value.
    Last edited by AlKey; 10-22-2013 at 09:12 PM.
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

  8. #8
    Registered User
    Join Date
    10-03-2013
    Location
    california
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: Macro to filter date based on a selection

    hi AlKey

    To answer your question, I am working on creating report generating macro for other users to help them save time. The filter data part is just one step in the process.

    In other words, some people just dont want the hassle of doing a report manually, step-by-step and just wanted to click a button.

+ 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. Macro - Count and Filter Column based on Date
    By foxy_420 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-07-2012, 10:52 AM
  2. [SOLVED] Macro to Filter and Delete Based on Date
    By Andrew.Trevayne in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-11-2012, 09:58 AM
  3. VBA to change filter for n pivots based on page filter selection for the first pivot
    By shama.arige in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-19-2011, 11:42 AM
  4. Filter records based on selection
    By chinaboy in forum Excel General
    Replies: 2
    Last Post: 10-28-2009, 12:46 PM
  5. [SOLVED] validation list - filter based on selection
    By jen in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-21-2006, 09:55 PM

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