Solved

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

Posted on 2013-06-07
3
316 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

910 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

23 Experts available now in Live!

Get 1:1 Help Now