通过将每个值连接起来的值循环放入一个单元格中

我想比较date以find重叠的date。

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

这是第二个function。 它将用于扩展部分重叠过程,比较事实上重叠的部分,并提供重叠范围之间的date序列,或者创build第一个date,其中短划线和最后date重叠。

代码我到目前为止:

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 

我本质上懒得把值的数组作为string来收集,所以我通常只使用一个Scripting.Dictionary,然后在完成时join这些键:

 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 

用下面的代码replace函数体:

 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 

我有一个非常糟糕的事情,我用来筛选date和查找重叠的date。 我只是分享这个 – 它不直接回答你的问题,但可能会给你一些从哪里开始的想法? 我将所有的开始date和结束date存储在数组中,并且只是通过这种方式来查看。 我通过将每个重叠存储在(0,0)维数组中find了最大的重叠(如果要存储所有的date,可以改变它)。 它也真的只适用于数组是否正常。 如果我现在需要这个,我只是将所有date转储到一个访问表中,只是查询,所以列表的顺序wouldnt它的问题。 或者我可以重写这个在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