用于date范围的UDF或标准IF函数?

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

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

因此,一旦1934年,1935年,1936年的序列开始,它将捕获1934年并迭代,直到差异不是1。

这个例子的结果是:

 1934 - 1936. 

一旦我创build了这些date范围,我将通过“ – ”分隔符使用文本到列来创build开始和结束date列。

在这方面的困难是比较另一列,并确保date是基于一个特定的标题。

因此:IF(标题1 =标题2,(IFdate2-date1 = 1,检查date3的增量 – date2 = 1,否则=date1))。

我可以尝试嵌套许多IF语句,但这会变得讨厌,加上不知道每个标题要比较多less个date。

迭代看起来好像在VBA中创build一个子或UDF会容易得多,但在这里(或其他地方)找不到任何可能有用的例子。

这将完成这项工作,它将把列F中的单元格输出到列G中。如果有一个年数范围(例如:1934-1935-1936),它将把它作为一个string放在一个单元格中。

 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将string更改为date格式

使用AGGREGATE函数检索第一组一天序列的开始和结束并不太困难。 以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)) 

first_group_one_day

如果要合并它们,则用第一个公式将第二个公式中的所有引用replace为H2。

我知道你需要一种方法来获取date“范围”,而我还没有得到“比较另一列”的事情

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

 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 (ie 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, ie 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 

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

如果你不介意把公式分解成几列,下面是一个关于如何提取开始和结束年的IF()语句示例:

查找开始和结束年份范围 -  Excel公式示例

=IF(B13>1,"",IF(B14<2,IF(C12="",A13,C12),IF(ISNUMBER(C12),C12&"-"&A14,A13&"-"&A14)))

说明

B栏 – 发现年份之间的差异

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

D列 – 如果长度> 4,则为Left() 4个字符

列E – Right()如果长度大于4,则为4个字符