+ Reply to Thread
Results 1 to 6 of 6

VBA to filter by dates, return values only if between certain dates

Hybrid View

Simon.xlsx VBA to filter by dates,... 05-24-2015, 06:22 AM
Simon.xlsx Re: VBA to filter by dates,... 05-24-2015, 06:32 AM
Jakobshavn Re: VBA to filter by dates,... 05-24-2015, 07:00 AM
Simon.xlsx Re: VBA to filter by dates,... 05-25-2015, 05:40 AM
Simon.xlsx Re: VBA to filter by dates,... 05-25-2015, 05:41 AM
Jakobshavn Re: VBA to filter by dates,... 05-25-2015, 10:08 AM
  1. #1
    Forum Contributor
    Join Date
    02-20-2014
    Location
    Ireland
    MS-Off Ver
    Excel 2013
    Posts
    108

    VBA to filter by dates, return values only if between certain dates

    Hi All,

    I have a list of employee names working on a job, beside their names is a start date and end date.

    Column A: Name
    Column B: Start Date
    Column C: End Date

    I want to write a macro that will look at the start date and end date, and if today's date falls between those dates, I want to copy and paste the name to a separate sheet

    Perhaps there is a way to use filter in my macro to achieve this result, as if it returns a blank, I don't want that blank to be copied to the new sheet.

    I can upload a test workbook if that will help.

    Thanks in advance

  2. #2
    Forum Contributor
    Join Date
    02-20-2014
    Location
    Ireland
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: VBA to filter by dates, return values only if between certain dates

    Further to the last post, I am able to filter the start date to "before or equal to" today

    and the end date to "After or equal to" today

    Recording this macro returns the following code

    ActiveSheet.ListObjects("Table_owssvr_1").Range.AutoFilter Field:=5, _
            Criteria1:="<=24/05/2015", Operator:=xlAnd
        ActiveSheet.ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6, _
            Criteria1:=">=24/05/2015", Operator:=xlAnd
    However I need the date "24/05/2015" to always be the current date.

    Any help be great

    Thanks

  3. #3
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: VBA to filter by dates, return values only if between certain dates

    Take a look at the attached workbook. It runs in manual calculation mode, so touch F9 before running the macro.
    Attached Files Attached Files
    Gary's Student

  4. #4
    Forum Contributor
    Join Date
    02-20-2014
    Location
    Ireland
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: VBA to filter by dates, return values only if between certain dates

    Hi,

    Thanks for your reply, however this seems a bit intricate for what I am looking for. As it will be inexperienced users using this macro.

    I think I have found a workaround my problem using the following code, however this is not running well for me

    Sub Dates()
    
    Dim DateCell As Date
    DateCell = Worksheets("Date").Range("A1")
    
        Worksheets("Histogram").ListObjects("Table_owssvr_1").Range.AutoFilter Field:=5, _
            Criteria1:="=<DateCell", Operator:=xlAnd
        Worksheets("Histogram").ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6, _
            Criteria1:="=>DateCell", Operator:=xlAnd
            
    End Sub
    When I run this, weather I enter physical dates, or look at the selected cell, the filters do not return anything, although if I filter manually it does.

    No idea what is causing this, I can record the macro, and when recording I get the results I want, but when I run the macro it returns empty.

    Any ideas whats causing this?

  5. #5
    Forum Contributor
    Join Date
    02-20-2014
    Location
    Ireland
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: VBA to filter by dates, return values only if between certain dates

    Hi,

    Thanks for your reply, however this seems a bit intricate for what I am looking for. As it will be inexperienced users using this macro.

    I think I have found a workaround my problem using the following code, however this is not running well for me

    Sub Dates()
    
    Dim DateCell As Date
    DateCell = Worksheets("Date").Range("A1")
    
        Worksheets("Histogram").ListObjects("Table_owssvr_1").Range.AutoFilter Field:=5, _
            Criteria1:="=<DateCell", Operator:=xlAnd
        Worksheets("Histogram").ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6, _
            Criteria1:="=>DateCell", Operator:=xlAnd
            
    End Sub
    When I run this, weather I enter physical dates, or look at the selected cell, the filters do not return anything, although if I filter manually it does.

    No idea what is causing this, I can record the macro, and when recording I get the results I want, but when I run the macro it returns empty.

    Any ideas whats causing this?

  6. #6
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: VBA to filter by dates, return values only if between certain dates

    Might be a simple syntax problem. try replacing:

    Criteria1:="=<DateCell"

    with:

    Criteria1:="=<" & DateCell

+ 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. Calculation of dates, return blank when one of the dates is not populated
    By brian_2me in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-15-2014, 10:59 AM
  2. Select Multiple Dates In Report Filter Pivot Table But Dates Are Not Static
    By biasedobserver in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-06-2014, 03:38 PM
  3. [SOLVED] TextBox values as filter criteria - dates
    By MKrop in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-09-2013, 03:49 AM
  4. [SOLVED] Filter between two dates and return the sum
    By Marvlin in forum Excel General
    Replies: 6
    Last Post: 04-24-2012, 02:03 PM
  5. Replies: 7
    Last Post: 01-16-2009, 05:04 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