You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel:提取列表中多组连续数据对应的首尾行表头(日期)

提取Excel日历中连续标记的首尾日期解决方案

嘿,我来帮你搞定这个Excel里提取连续标记(x)首尾日期的需求!先把你的示例数据整理成清晰的表格,方便我们理解:

原始表格结构

日期TomDickHarry
1x
2x
3x
4x
5x
6x
7xx
8xx
9xx

我们的目标是提取每个人的每组连续x对应的首个和最后一个日期,比如Tom有两组连续标记:1-3、7-9;Dick是4-6;Harry是7-9。下面给你两种实用的解决方法:

方法一:公式法(适合小规模数据,无需代码)

如果你的数据量不大,用Excel内置公式就能搞定,分两步走:

步骤1:添加辅助列区分连续组

在表格右侧新增辅助列(比如D列对应Tom),输入以下公式并下拉:

=IF(B2="", "", IF(B1=B2, D1, D1+1))

把这个公式复制到对应Dick、Harry的辅助列(E、F列),这样每组连续的x会被标记为相同的组号,新的连续组号自动+1。

步骤2:提取每组首尾日期

用UNIQUE获取每个人的唯一组号,再结合MINIFS和MAXIFS提取对应组的最小/最大日期:

  • 比如针对Tom,在G2输入=UNIQUE(D2:D10)获取所有组号
  • H2(首日期):=MINIFS($A$2:$A$10, $B$2:$B$10, "x", $D$2:$D$10, G2)
  • I2(尾日期):=MAXIFS($A$2:$A$10, $B$2:$B$10, "x", $D$2:$D$10, G2)

下拉公式就能得到Tom的所有连续区间,同理处理Dick和Harry即可。

如果你用的是Excel 365,还可以用动态数组公式一次性输出结果,不用手动下拉:

=LET(
    name, "Tom",
    col, MATCH(name, $1:$1, 0),
    data, $A$2:$A$10*($B$2:$J$10="x"),
    groups, SCAN(0, data, LAMBDA(a,b, IF(b=0, a, a+(a=0 OR OFFSET(data, -1, 0)=0)))),
    unique_groups, FILTER(UNIQUE(groups), groups>0),
    start_dates, MINIFS($A$2:$A$10, groups, unique_groups),
    end_dates, MAXIFS($A$2:$A$10, groups, unique_groups),
    HSTACK(REPT(name, ROWS(unique_groups)), start_dates, end_dates)
)

把"Tom"替换成其他姓名,就能直接得到对应人员的所有连续日期区间。

方法二:VBA宏(适合批量处理大量数据)

如果你的表格有几十上百个人员或日期,手动公式太繁琐,写个VBA宏就能自动完成提取:

  1. 打开Excel,按Alt+F11打开VBA编辑器
  2. 右键点击当前工作簿,选择「插入」→「模块」
  3. 粘贴以下代码:
Sub ExtractDateRanges()
    Dim ws As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim i As Long, j As Long
    Dim startDate As Long, endDate As Long
    Dim outputRow As Long
    
    Set ws = ActiveSheet
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
    outputRow = 2 '输出从新工作表第2行开始,表头手动添加
    
    '创建输出工作表(如果已存在则先删除)
    On Error Resume Next
    Sheets("DateRanges").Delete
    On Error GoTo 0
    Sheets.Add.Name = "DateRanges"
    With Sheets("DateRanges")
        .Cells(1, 1) = "姓名"
        .Cells(1, 2) = "开始日期"
        .Cells(1, 3) = "结束日期"
    End With
    
    '遍历每一列(每个人员)
    For j = 2 To lastCol
        startDate = 0
        '遍历每一行(每个日期)
        For i = 2 To lastRow
            If ws.Cells(i, j).Value = "x" Then
                If startDate = 0 Then startDate = ws.Cells(i, 1).Value
                endDate = ws.Cells(i, 1).Value
            Else
                '如果之前有连续标记,写入结果
                If startDate <> 0 Then
                    With Sheets("DateRanges")
                        .Cells(outputRow, 1) = ws.Cells(1, j).Value
                        .Cells(outputRow, 2) = startDate
                        .Cells(outputRow, 3) = endDate
                    End With
                    outputRow = outputRow + 1
                    startDate = 0
                End If
            End If
        Next i
        '处理最后一组未写入的连续标记
        If startDate <> 0 Then
            With Sheets("DateRanges")
                .Cells(outputRow, 1) = ws.Cells(1, j).Value
                .Cells(outputRow, 2) = startDate
                .Cells(outputRow, 3) = endDate
            End With
            outputRow = outputRow + 1
        End If
    Next j
    
    MsgBox "提取完成!结果已保存到「DateRanges」工作表。"
End Sub
  1. 按F5运行宏,等待提示完成后,就能看到自动生成的结果工作表。

最终示例输出

按照你的原始数据,最终提取结果如下:

姓名开始日期结束日期
Tom13
Tom79
Dick46
Harry79

内容的提问来源于stack exchange,提问作者Dan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:50:14