【问题标题】:UDF or standard IF function for date range?日期范围的 UDF 或标准 IF 函数?
【发布时间】:2016-09-23 16:32:16
【问题描述】:

我正在尝试从列中的数字创建一个范围,并创建一个范围。例如,

column F: 
1899 
1912 
1915 
1918 
1929 
1934 
1935 
1936
... 

因此,一旦 1934、1935、1936 序列开始,它就会捕获 1934 并迭代直到差值不为 1。

这个例子的结果是:

1934 - 1936. 

创建这些日期范围后,我将使用“-”分隔符的文本列来创建开始和结束日期列。

其中的困难在于比较另一列并确保日期基于特定标题。

所以:IF(标题 1 = 标题 2,(IF 日期 2 - 日期 1 = 1,递增到检查日期 3 - 日期 2 = 1,否则 = 日期 1))。

我可以尝试嵌套许多 IF 语句,但这会很麻烦,而且不知道每个标题要比较多少日期。

迭代似乎通过在 VBA 中创建子或 UDF 会容易得多,但我在这里(或其他地方)没有找到任何可能有用的示例。

【问题讨论】:

  • 您可以使用 [excel-formula] 执行此操作,但如果 1 天排序停止然后重新启动,您将遇到问题。作为一个带有可选参数的 UDf 可能更好,该参数告诉它使用哪一组 1 天日期分隔。
  • 您是否已经解决了“从列中的数字创建范围” 问题?另外,我不明白“比较另一列” 是什么:您介意添加一个示例吗?
  • 在 VBA 中创建 sub 或 UDF,但我在这里没有找到任何示例——此时您可以做的最好的事情是 尝试创建一个,并在考虑到这一点的情况下修改您的问题,让我们知道您遇到的问题/错误。通常没有针对个别问题的现成解决方案,这完全是关于修改/调整和使用内置函数和对象模型来为您带来优势,以解决您的特定问题。
  • 在我看来,“比较另一列”本质上就像使用密钥一样。例如:标题1:日期1899 1912 1915;标题2:1918 1929 1934 1935 1936
  • 如果你得到这个答案作为第一组一天序列的开始和停止的公式,你会立即问如何获得第二组吗?

标签: excel vba if-statement iteration udf


【解决方案1】:

这将完成这项工作,它将在 F 列中获取单元格并在 G 列中打印您的输出。如果有一个年份范围(例如:1934-1935-1936),它将把它放在一个单元格中一个字符串。

Sub range_dates()
Dim cel As Range
Dim iRow As Long, n As Long
Dim title As String, nextTitle As String
iRow = 2
n = 0
For i = 2 To 15
    Set cel = Cells(i, "F")
    title = Cells(i, "E").Value
    nextTitle = Cells(i + 1, "E").Value
    diff = Cells(i + 1, "F").Value - cel.Value

    If diff = 1 And title = nextTitle Then
        firstDate = cel.Value - n
        n = n + 1
    Else
        Cells(iRow, "H").Value = cel.Value
        Cells(iRow, "G").Value = title
        If n > 0 Then
            Cells(iRow, "H") = "'" & firstDate & " - " & cel.Value
            n = 0
        End If
        iRow = iRow + 1
    End If
Next
End Sub

我知道这段代码不是最优化的,但到目前为止,它是我必须分享的。随时改进它:)

编辑:我添加了标题条件并更改了输出以防止 excel 将字符串更改为日期格式

【讨论】:

  • 这就是我需要的。我已经完成了每个人提供的所有其他工作,但这似乎是我正在寻找的最接近的代表。我要添加到您所拥有的唯一内容是:如果 diff = 1 "And Cell D1=D2...D2=D3...etc,否则,移动到下一个 D 并带有新标题"跨度>
  • 我想我知道你的标题了..见编辑。它将验证您是否具有相同的标题(E 列)并打印标题和年份的输出
【解决方案2】:

使用AGGREGATE function 检索第一组单日序列的开始和结束并不难。以H2:I2为标准公式,

'in H2
=INDEX(F$2:F$15,AGGREGATE(15,6,ROW($1:$14)/((F$3:F$16-$F$2:$F$15)=1), 1))    
'in I2
=INDEX(INDEX(F:F, MATCH(H2,F:F, 0)):F$15,AGGREGATE(15,6,ROW($1:$14)/((INDEX(F:F, MATCH(H2,F:F, 0)+1):F$16-INDEX(F:F, MATCH(H2,F:F, 0)):$F$15)<>1), 1))

如果您想组合它们,请将第二个公式中对 H2 的所有引用替换为第一个公式。

【讨论】:

    【解决方案3】:

    我知道您需要一种方法来获取日期“范围”,而我仍然没有得到“比较另一列”的东西

    至于第一个问题,你可以试试这个(注释)代码:

    Option Explicit
    
    Sub main()
        Dim fictitiousRng As Range, area As Range
        Dim arr As Variant
        Dim iCell As Long
    
        With Worksheets("numbers") '<--| reference your worksheet (change "numbers" to your actual worksheet name)
            With Intersect(.UsedRange, .Columns("F")).SpecialCells(xlCellTypeConstants, xlNumbers) '<-- reference its column "F" non blank cells with numeric data only
                With .Offset(, .Parent.UsedRange.Columns.Count) '<--| reference referenced range offsetted 1 colum to the right
                    .FormulaR1C1 = "=""A"" & RC6" '<--| fill its content with a "fictitiuous" range address by placing an "A" in front of corresponding number in column "F"
                    Set fictitiousRng = Range("B1") ' set a initial range for 'fictitiousRng' not to bother for subsequent first call of 'Union()' method
                    For Each area In .Areas '<--| loop through referenced range areas (i.e. cells "groups")
                        For iCell = 2 To area.Cells.Count '<--| loop through current area cells
                            Set fictitiousRng = Union(fictitiousRng, Range(area.Cells(iCell, 1).value)) '<--| update 'fictitiousRng' with the cell corresponding to the current fictitious range address
                        Next iCell
                    Next area
                    arr = Split(Replace(Replace(Intersect(fictitiousRng, Columns(1)).Address(False, False), "A", ""), ":", " - "), ",") '<--| get the fictitious range address, remove "A"s and substitute "." with "-" and then write final "numbers" into an array
                    .ClearContents '<-- clear referenced range, i.e. cells with fictitious addresses
                    .Cells(1, 1).Resize(UBound(arr) + 1).value = Application.Transpose(arr) '<--| write array content into referenced range cells starting from its first cell
                End With
            End With
        End With
    End Sub
    

    它将日期“范围”写入同一列“F”日期工作表的第一个未使用列中

    【讨论】:

      【解决方案4】:

      如果您不介意将公式分成几列,这里有一个 IF() 语句示例,说明如何提取开始和结束年份:

      =IF(B13&gt;1,"",IF(B14&lt;2,IF(C12="",A13,C12),IF(ISNUMBER(C12),C12&amp;"-"&amp;A14,A13&amp;"-"&amp;A14)))

      说明

      B 列 - 查找年份之间的差异

      C 列 - 优雅的nested IF() 公式;)

      列 D - Left() 4 个字符,如果长度 > 4

      列 E - Right() 4 个字符,如果长度 > 4

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-28
        • 2020-10-24
        • 1970-01-01
        • 2021-10-30
        • 1970-01-01
        • 2015-04-26
        • 1970-01-01
        • 2014-01-07
        相关资源
        最近更新 更多