Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Macro for filtering by a range of dates

Posted on 2012-03-18
4
Medium Priority
?
289 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 93

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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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…

916 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