Solved

Look in list of dates and see if date falls within start and end

Posted on 2013-06-07
3
305 Views
Last Modified: 2013-06-09
I have a set of Begin and End Dates in two columns.  I want to lookup a begin and end date based on where the  current date falls <=  Beg and < =  End and store those dates in two variables vBeg and vEnd.

How do I do this?

Thanks

Glen
0
Comment
Question by:GPSPOW
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 39230468
I have done something more general as you did not post an example, but I hope you will be able to adapt this.  You could make this much more efficient if you have  lots of date ranges and you can guarantee the start dates are in sort order, but this should work for most scenarios.  It stops as soon as it finds a match.
Option Explicit

Private Sub LookupDates()

    Dim rngStartDate As Range
    Dim rngEndDate As Range
    
    Set rngStartDate = ActiveSheet.Columns(1).Cells
    Set rngEndDate = ActiveSheet.Columns(2).Cells
    
    Dim dtCurrent As Date
    
    dtCurrent = Date

    ' skip the header and trim the range to the last cell with data
    Set rngStartDate = ActiveSheet.Range(rngStartDate.Cells(2, 1), rngStartDate.Cells(rngStartDate.Cells.Count).End(xlUp))
    Set rngEndDate = rngEndDate.Resize(rngStartDate.Rows.Count, 1).Offset(1, 0)
    
    Dim vBeg As Variant
    Dim vEnd As Variant
    
    vBeg = 0
    vEnd = 0
    
    Dim i As Long
    
    For i = 1 To rngStartDate.Rows.Count
        
        If (rngStartDate.Cells(i, 1).Value <= dtCurrent) And (rngEndDate.Cells(i, 1).Value >= dtCurrent) Then
            vBeg = rngStartDate.Cells(i, 1).Value
            vEnd = rngEndDate.Cells(i, 1).Value
            Exit For
        End If
    
    Next i
    
    If (vBeg > 0) And (vEnd > 0) Then
        MsgBox "Found current date between " & vBeg & " and " & vEnd
    Else
        MsgBox "Did not find date"
    End If
    

End Sub

Open in new window

DateLookup.xlsm
0
 

Author Closing Comment

by:GPSPOW
ID: 39232473
Thank you
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39233335
Glad it helped. Just to expand on what I mentioned about performance, it's worth saying that if your list  has contiguous and non-overlapping date ranges in order, you don't need to worry about the end date at all - just find the first start date that is less than or equal to today.  That is exactly what the worksheet function MATCH does when the third parameter is '1', or VLOOKUP when the 4th parameter is 'TRUE'. My  EE article http://rdsrc.us/aaHbiZhttp://rdsrc.us/aaHbiZ gives more details.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

744 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

12 Experts available now in Live!

Get 1:1 Help Now