Solved

Macro for filtering by a range of dates

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

710 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