2016-09-09 8 views
1

重複するものを見つけるために日付を比較したいと思います。ループスルー値を連結して1つのセルに入れます

私が作成した最初の関数は、完全な重なり、部分的な重なり、または重なりのないことをチェックします。

これは2番目の機能です。実際に重なり合っているものを比較し、重複した範囲の間の日付のシーケンスを提供するか、またはダッシュと最後の日付が重なる最初の日付を作成するために、部分的重複プロセスを拡張するために使用されます。

コードは、私がこれまで持っている:

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番を得て、これを私の違いとしてforループに使うべきです。各反復について、私はSD2に1を加えたいと思う。 –

答えて

0

を行うために、VBAでこれを書き換えることができ、私は文字列として値の配列を収集について、本質的に怠惰なので、私は通常ちょうどScripting.Dictionaryを使用して、私は「ときにだけキーに参加しますm done:

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)次元の配列に格納することで最大の重なりを見つけます(すべての日付を保存したい場合はこれを変更してください)。配列が整然と並んでいる場合にのみ実際に動作します。私は今これが必要な場合、私はアクセステーブルとすべての日付をダンプして、リストの順序が重要でないようにするだけです。または私は同じこと

  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 
関連する問題