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

x
?
Solved

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

Posted on 2013-06-07
3
Medium Priority
?
364 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

688 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