Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 2 / February 2008

Tip: Looking for answers? Try searching our database.

Overlapping Date Ranges

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jose - 21 Feb 2008 14:38 GMT
Hi,

I have 4 columns, 2 start date and 2 end dates.  In most cases, both are the
same.  In some, the start date is the same and the end date is different and
vice versa, how can I accurately get the number of days overlap?

Signature

I need so much help, but right now, some Excel help will suffice!!

Douglas J. Steele - 21 Feb 2008 15:07 GMT
Your signature talks about Excel help, but you've posted to a newsgroup
related to Access, the database product that's part of Office Professional.

If your question really is about Excel, you'd be best off reposting to a
newsgroup related to Excel.

If it's an Access question, what exactly do you mean by "the number of days
overlap"?

You can calculate the number of days between a given pair of start and end
dates using DateDiff("d", [Start], [End])

If what you're trying to do is get the difference between the later of the
two start dates and the earlier of the two end dates, try

DateDiff("d", IIf([Start1] < [Start2], [Start2], [Start1]), IIf([End1] <
[End2], [End1], [End2]))

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

> Hi,
>
[quoted text clipped - 3 lines]
> and
> vice versa, how can I accurately get the number of days overlap?
James A. Fortune - 22 Feb 2008 01:35 GMT
> Hi,
>
> I have 4 columns, 2 start date and 2 end dates.  In most cases, both are the
> same.  In some, the start date is the same and the end date is different and
> vice versa, how can I accurately get the number of days overlap?

Someone must have been wondering where I was.  Try:

http://groups.google.com/group/comp.databases.ms-access/msg/9df7500e40e39155

It should work in an Excel Macro also.  Something like (air code):

Dim Wks As Worksheet
Dim lngLastRow As Long
Dim lngI As Long

Set Wks = ActiveSheet
With Wks
  lngLastRow = .Cells(.Rows.Count, "A".End(xlUp).Row
End With
For lngI = 2 To lngLastRow 'Headings on row 1
  With wks
    If Not IsEmpty(.Cells(lngI, "A").Value) And Not
IsEmpty(.Cells(lngI, "B").Value) And Not IsEmpty(.Cells(lngI,
"C").Value) And Not IsEmpty(.Cells(lngI, "D").Value) Then
      If IsDate(.Cells(lngI, "A").Value) And IsDate(.Cells(lngI,
"B").Value) And IsDate(.Cells(lngI, "C").Value) And IsDate(.Cells(lngI,
"D").Value) Then
        .Cells(lngI, "E").Value = DateIntersection(CDate(.Cells(lngI,
"A").Value), CDate(.Cells(lngI, "B").Value), CDate(.Cells(lngI,
"C").Value), CDate(.Cells(lngI, "D").Value))
      End If
    End If
  End With
Next lngI

'You can put DateIntersection code here

Columns A through D should be formatted as dates.  The macro will put
the number of days of overlap in column E.  This example assumes that
columns A and B contain a start and end date and that columns C and D
contain another start and end date.  You'll probably have to edit out
any carriage returns caused by line wrap.

James A. Fortune
MPAPoster@FortuneJames.com
gllincoln - 22 Feb 2008 04:57 GMT
Hi Jose,

Interesting puzzle - is a VBA Function solution okay?  

I ran some randomly generated dates through this and I think this function may meet your stated objective. I haven't methodically tested it with every possible combination.

I'm not happy with this solution - it's ugly, not the least bit elegant - sheer brute force. I'd like to see how a different approach might clean it up - maybe a strong programmer lurking about will give this a whirl and show me how I shoulda coulda done it better. This is similar to a lot of scheduling type programming challenges so it's a worthwhile knot to untangle in a reusable, more extensible manner.

'sd1 = start one
'sd2 = start two
'ed1 = end one
'ed2 = end two

Function myOverLap(ByVal sd1 As Date, ByVal sd2 As Date, ByVal ed1 As Date, ByVal ed2 As Date) As Integer
   'same start
   If sd1 = sd2 Then
       'same finish
       If ed1 = ed2 Then
           'complete overlap, calc the days
           myOverLap = ed1 - sd1 + 1
       Else
           'same start two ends first
           If ed1 > ed2 Then
               myOverLap = (ed2 - sd2) + 1
           Else
               'same start one ends first
               myOverLap = (ed1 - sd1) + 1
           End If
       End If
   Else
       'different start one before two
       If sd1 < sd2 Then
           'one before two same end
           If ed1 = ed2 Then
               myOverLap = (ed1 - sd2) + 1
           Else
               'one ends before two starts
               If sd2 > ed1 Then
                   myOverLap = 0
               Else
                   'one starts first - one ends first
                   If ed1 < ed2 Then
                       myOverLap = (ed1 - sd2) + 1
                   Else
                       'one starts first - two ends first
                       myOverLap = (ed2 - sd2) + 1
                   End If
               End If
           End If
       Else
           'two starts first - same end
           If ed1 = ed2 Then
               myOverLap = (ed1 - sd1) + 1
           'one starts after two ends
           ElseIf sd1 > ed2 Then
               myOverLap = 0
           Else
               'two starts first - one ends first
               If ed1 < ed2 Then
                   myOverLap = (ed1 - sd1) + 1
               Else
                   'two starts first - two ends first
                   myOverLap = (ed2 - sd1) + 1
                   
               End If
           End If
       End If
   End If
   
End Function

==================

Hope this helps...
Gordon

> Hi,
>
> I have 4 columns, 2 start date and 2 end dates.  In most cases, both are the
> same.  In some, the start date is the same and the end date is different and
> vice versa, how can I accurately get the number of days overlap?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.