Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Macro for filtering by a range of dates

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

705 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