【问题标题】:Loop through values concatenating each to put in one cell循环连接每个值以放入一个单元格
【发布时间】:2016-09-09 18:12:49
【问题描述】:

我想比较日期以找到重叠的日期。

我创建的第一个函数检查完全重叠、部分重叠或无重叠。

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

到目前为止我的代码:

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

【问题讨论】:

  • 只有年份重叠还是日期重叠才需要这个?
  • 仅限年份,从技术上讲,所有使用的年份都是严格的数字。我最近发现我无法将 1900 年之前的任何事物作为年份进行比较,因此我只是将所有内容都转换为数字进行比较。所以对于这个例子,我应该得到数字 72 作为我的差异,并将其用作我的差异 for 循环。对于每次迭代,我想将 1 添加到 SD2。

标签: excel vba loops concatenation


【解决方案1】:

我天生懒惰将值数组收集为字符串,所以我通常只使用 Scripting.Dictionary,然后在完成后加入键:

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

【讨论】:

    【解决方案2】:

    用以下几行替换函数体:

    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
    

    【讨论】:

      【解决方案3】:

      我有一个非常糟糕的 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
      

      【讨论】:

        猜你喜欢
        • 2014-06-11
        • 2017-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-20
        • 1970-01-01
        相关资源
        最近更新 更多