+ Reply to Thread
Results 1 to 12 of 12

Filter Column Based On Date Range

  1. #1
    Registered User
    Join Date
    08-28-2012
    Location
    Indiana
    MS-Off Ver
    It changes depending on the job.
    Posts
    36

    Cool Filter Column Based On Date Range

    Hello!
    I consider myself a novice VBA user. I have used a little bit here and there over the years, however past the simple things... I'm in over my head, but willing to learn!

    I tried to find an answer before I posted this, I'm not sure if I'm asking Google the correct questions. I am hoping someone here will be able to help, or even point me in the direction I should go.

    I've uploaded a sample file. I have the code to take the Raw Data Tab and copy it, renaming the tabs with the data from the Information Tab, Column A. What I need to be able to automate with VBA is on each Month Tab, filter column F based on the month the sheet is named for. On the information tab I have each month's date range.

    If the data in Column A on the Information Tab was going to always be the same, I would be able to figure it out. I haven't clue one how to make it work with the data being a variable.

    I would appreciate any help and give you many thinks in advance!
    Attached Files Attached Files
    ~ Spoony

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,144

    Re: Filter Column Based On Date Range

    This is the VBA:

    Please Login or Register  to view this content.
    But you could also just use FILTER if you are using a version that supports it - just enter the month name into a cell to make the reference easier.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hello, try this ...


    According to the attachment an Excel basics VBA demonstration exporting 'Raw Data' matching with the next sheets :

    PHP Code: 
    Sub Demo1()
            
    Dim S&
        
    With Sheet2
            
    For = .Index 1 To Worksheets.Count
               
    .[K2].Formula "1 " Worksheets(S).Name
               
    .[K2].Formula "=MONTH(F2)=" Month(.[K2])
                
    Worksheets(S).UsedRange.Clear
               
    .[A1].CurrentRegion.AdvancedFilter 2, .[K1:K2], Worksheets(S).[A1]
            
    Next
               
    .[K2].Clear
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  4. #4
    Registered User
    Join Date
    08-28-2012
    Location
    Indiana
    MS-Off Ver
    It changes depending on the job.
    Posts
    36

    Re: Hello, try this ...

    Oh my goodness! You guys are wonderful! Both codes do exactly what I need done! I am absolutely amazed and can not thank you enough for the help!

    If we were in the same room I would give you both a giant hug!

  5. #5
    Registered User
    Join Date
    08-28-2012
    Location
    Indiana
    MS-Off Ver
    It changes depending on the job.
    Posts
    36

    Re: Filter Column Based On Date Range

    Soooo..... I have a glitch and do not know how to resolve it. I put the code in my report and it works perfectly! The issue is the year. In October they are going to want to run the run the report for October, November, December, and January. When I tested, the report only does the current year. How do I adjust to have multiple years?

    Any assistance is appreciated!

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,144

    Re: Filter Column Based On Date Range

    This should do it.... (note that I just edited the code a bit to make testing it work now and not just when it is used in October..)

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 05-12-2023 at 10:35 AM.

  7. #7
    Registered User
    Join Date
    08-28-2012
    Location
    Indiana
    MS-Off Ver
    It changes depending on the job.
    Posts
    36

    Re: Filter Column Based On Date Range

    WooHoo!!! * Doing a Happy Dance!!! * That did it! I can not thank you enough!!!!

  8. #8
    Registered User
    Join Date
    08-28-2012
    Location
    Indiana
    MS-Off Ver
    It changes depending on the job.
    Posts
    36

    Re: Filter Column Based On Date Range

    I'm sorry to be back here again bugging you'all about the same subject. I noticed an issue, and I don't know how to fix it. If a date is the last day of the month, it's being excluded. It took me a while to figure it out. Can anyone tell me how I can fix it, or if I have something wrong in the code?

    This is what I'm using:
    Sub FilterDates()
    Dim i As Integer
    i = 0
    If DateValue(ActiveSheet.Name & " 1, " & Year(Date)) < Application.EDate(Date, -3) Then i = 1
    ActiveSheet.UsedRange.AutoFilter
    ActiveSheet.UsedRange.AutoFilter Field:=6, Criteria1:=">=" & DateValue(ActiveSheet.Name & " 1, " & Year(Date) + i) _
    , Operator:=xlAnd, Criteria2:="<=" & Application.EDate(DateValue(ActiveSheet.Name & " 1, " & Year(Date) + i), 1) - 1
    End Sub

    I appreciate any assistance someone can give me!

  9. #9
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,144

    Re: Filter Column Based On Date Range

    Change to this - I think you may be getting date/times rather than simple dates.

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    08-28-2012
    Location
    Indiana
    MS-Off Ver
    It changes depending on the job.
    Posts
    36

    Re: Filter Column Based On Date Range

    BERNIE IS AWESOME!!!! I think that did it! It's our busy season so I didn't have time to test yesterday. I'll do some more testing tomorrow! THANK YOU!!! THANK YOU!!!

    I am a little confused though.... comparing the two they look the same. What am I missing?

  11. #11
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,144

    Re: Filter Column Based On Date Range

    Dates are stored as integer numbers, and times are decimal numbers between 0 and 1.

    The comparison is actually against the first of the month rather than the last day, to capture date/time combinations that occur on the last day of the month but are actually greater than the date integer.

  12. #12
    Registered User
    Join Date
    08-28-2012
    Location
    Indiana
    MS-Off Ver
    It changes depending on the job.
    Posts
    36

    Re: Filter Column Based On Date Range

    You are amazing Bernie! I would never have caught that. Thank you for your patience and excellent explanation.

+ 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. Replies: 2
    Last Post: 09-30-2022, 09:28 AM
  2. Filter Range Based on Unique Values in a Column
    By tjebme in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-14-2021, 08:35 PM
  3. Select column to filter based on date using vba
    By wetgoose69 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-23-2020, 01:42 PM
  4. [SOLVED] Filter Pivot table based on max date in column
    By dpelizzari in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-22-2015, 03:30 AM
  5. Filter Pivot based on Cell Value - Date Range
    By mfaustin in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 07-26-2013, 08:47 AM
  6. Daily Pivot Filter based on Date range specified in cells
    By swicked in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-02-2012, 01:23 AM
  7. [SOLVED] Date Filter based on Cell Input - single date or range of dates
    By babaloo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-23-2012, 10:32 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