2016-09-09 12 views
1

我想比较日期以找到重叠的日期。通过循环值连接每个放入一个单元格

我创建的第一个函数检查完整重叠,部分重叠或不重叠。

这是第二个功能。它将用于扩展部分重叠过程,比较实际重叠的内容,并提供重叠范围之间的日期序列,或者创建第一个日期与短划线和最后日期重叠的日期。

代码我到目前为止有:

Public Function partialdates(SD1 As Integer, ED1 As Integer, SD2 As Integer, ED2 As Integer) As String 

'This function will be expanded, but the first If statement takes 1 case such that the first set of dates overlaps, but not beginning with the start date: SD1 = 1885 ED1 = 1969, SD2 = 1897 ED2 = 1972 

    Dim i As Integer 
    Dim years As Integer 
    Dim difference As Integer 

    If SD1 < SD2 And SD1 <= ED2 And ED1 <= ED2 And ED1 >= SD2 Then 
     difference = ED1 - SD2 
     For i = 1 To difference 

' I need help with this, to create a sequence of years that will be what are 
' overlapped, such as 1897, 1898, 1899...etc 

      years = years & ", " + 1   
     Next 
     partialdates = years 
    End If 
End Function 
+0

你需要这个只用于多年重叠或日期重叠? – Karpak

+0

只有多年,技术上所有使用的年份都是严格的数字。最近我发现1900年之前我无法比较任何东西,所以我只是将所有东西都转换成数字进行比较。因此,对于这个例子,我应该得到数字72为我的差异,并使用它作为我的差异循环。对于每次迭代,我想将1添加到SD2。 –

回答

0

我天生懒惰有关收集值的数组作为字符串,所以我通常只使用一个和的Scripting.Dictionary然后刚刚加入的钥匙,当我” m完成:

Public Function partialdates(SD1 As Integer, ED1 As Integer, SD2 As Integer, ED2 As Integer) As String 
    If SD1 < SD2 And SD1 <= ED2 And ED1 <= ED2 And ED1 >= SD2 Then 
     Dim difference As Integer 
     difference = ED1 - SD2 

     Dim years As Integer 
     years = SD2 
     With CreateObject("Scripting.Dictionary") 
      Dim i As Integer 
      For i = 1 To difference 
       .Add years, vbNull 
       years = years + 1 
      Next 
      partialdates = Join(.Keys, ", ") 
     End With 
    End If 
End Function 
0

用以下行替换函数体:

Dim i As Integer 
Dim difference As Integer 

If SD1 < SD2 And SD1 <= ED2 And ED1 <= ED2 And ED1 >= SD2 Then 
    difference = ED1 - SD2 
    partialdates = "" 
    For i = 1 To difference 

' I need help with this, to create a sequence of years that will be what are 
' overlapped, such as 1897, 1898, 1899...etc 

     partialdates = partialdates & IIf(partialdates = "", "", ", ") & (SD2 + i) 
    Next 
End If 
0

我有一个非常糟糕的VB的事情我曾经使用过的日期进行筛选,找到重叠的日期。我只是分享这个 - 它不直接回答你的问题,但可能会让你知道从哪里开始?我将所有开始日期和结束日期存储到数组中,并以此方式查看它。我通过将每个重叠存储在(0,0)维数组中找到最大的重叠(如果要存储所有日期,您可以改变这一点)。它也真的只适用于数组是否正常。如果我现在需要这个,我只需要将所有日期转储到访问表中,然后查询,以便列表的顺序无关紧要。我也可以在VBA中重写这个做同样的事情

  Dim ChkArray(0,0) as date 

      For l = LBound(UACFArray, 1) To UBound(UACFArray, 1) 
       For m = LBound(UACFArray, 2) To UBound(UACFArray, 2) 

       Select Case StartDate 

        Case Is = UACFArray(l, 0) 
         EndDate = UACFArray(l, m) 

        Case Is <> UACFArray(l, 0) 

         If StartDate > UACFArray(l, 0) And StartDate < UACFArray(l, m) Then 
          For c = LBound(ChkArray, 1) To UBound(ChkArray, 1) 

           ChkArray(c, 0) = UACFArray(l, 0) 
           ChkArray(c, 1) = UACFArray(l, m) 

          Next c 

         End If 
       End Select 

       Next m 
      Next l