Solved

Macro for filtering by a range of dates

Posted on 2012-03-18
4
286 Views
Last Modified: 2012-07-30
I have a spreadsheet with approximately 10,000 rows of data. Column A has tentative dates and Column B has actual dates.

I need a macro where i can set the lower and upper range of the dates, within the macro not by user input and filter according to that range. Column A and B are not dependent on each other.

Thanks in advance for any suggestions
0
Comment
Question by:dgd1212
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 18

Expert Comment

by:krishnakrkc
ID: 37734846
Hi,

This would give the output if the dates from either Col A or Col B falls within the range.

Sub kTest()
    
    Dim ka, k(), i As Long, n As Long, Flg As Boolean
    Dim dtStart As Date, dtEnd  As Date
    Dim sDt As Long, eDt As Long, UB1 As Long
    
    ka = Intersect(ActiveSheet.UsedRange, Range("a:b")).Value2
    
    dtStart = DateSerial(2000, 1, 1)    'year,month,day
    dtEnd = DateSerial(2012, 3, 1)      'year,month,day
    
    sDt = CLng(dtStart): eDt = CLng(dtEnd)
    UB1 = UBound(ka, 1)
    
    ReDim k(1 To UB1, 1 To 2)
    
    For i = 2 To UB1
        Flg = ((ka(i, 1) >= sDt) * (ka(i, 1) <= eDt)) + ((ka(i, 2) >= sDt) * (ka(i, 2) <= eDt))
        If Flg Then
            n = n + 1
            k(n, 1) = ka(i, 1): k(n, 2) = ka(i, 2)
        End If
    Next
    
    If n Then
        With Range("e2") '<< adjust this range
            .CurrentRegion.Resize(, 2).Offset(1).ClearContents
            .Resize(n, 2) = k
            .Resize(n, 2).NumberFormat = "dd-mmm-yy"
        End With
    End If
    
End Sub

Open in new window


Kris
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 37734941
It's not at all clear what you want.

Do you want to see only rows where both dates fall within your date range, or where at least one of the dates falls within the range?

By "filter", do you mean the standard AutoFilter functionality where the rows are hidden, or do you actually want to remove the rows from the set?
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 37734970
Assuming you mean normal filtering, this would apply it if both dates must fall within the range:


Sub FilterOnBoth()
    
    Const StartDate As Date = #1/1/2012#
    Const EndDate As Date = #12/31/2012#
    
    With [a1]
        .AutoFilter
        .AutoFilter 1, ">=" & StartDate, xlAnd, "<=" & EndDate
        .AutoFilter 2, ">=" & StartDate, xlAnd, "<=" & EndDate
    End With
    
End Sub

Open in new window



If you want to filter on either date falling within the range, you need a helper column:


Sub FilterOnEither()
    
    Dim LastR As Long, LastC As Long
    
    Const StartDate As Date = #1/1/2012#
    Const EndDate As Date = #12/31/2012#
    
    With ActiveSheet
        .[a1].AutoFilter
        LastR = .Cells(.Rows.Count, 1).End(xlUp).Row
        LastC = .Cells(1, .Columns.Count).End(xlToLeft).Column
        .Cells(1, LastC + 1) = "Filter"
        .Cells(2, LastC + 1).Resize(LastR - 1, 1).Formula = "=OR(AND(A2>=DATEVALUE(""" & StartDate & """)," & _
            "A2<=DATEVALUE(""" & EndDate & """)),AND(B2>=DATEVALUE(""" & StartDate & """)," & _
            "B2<=DATEVALUE(""" & EndDate & """)))"
        With .[a1]
            .AutoFilter
            .AutoFilter LastC + 1, True, xlAnd
        End With
    End With
    
End Sub

Open in new window

0
 

Author Closing Comment

by:dgd1212
ID: 37750514
Thank You!
0

Featured Post

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question