Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

739 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