2016-09-09 88 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 
相關問題