Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

822 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