Solved

Macro for filtering by a range of dates

Posted on 2012-03-18
4
281 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
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…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now