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

Excel含空白单元格列提取最新日期:内置函数或VBA实现方法

解决Excel含空白单元格的日期列取最新日期问题

一、内置函数方案

MAX函数本身就会自动忽略空白单元格,直接使用即可。假设日期列是A列,公式如下:
=MAX(A:A)

如果列中存在非日期文本导致公式报错,可以用更稳妥的方案:

  • AGGREGATE函数(推荐):忽略错误值、空白和隐藏行,自动筛选日期取最大值
    =AGGREGATE(4,6,A:A)
    参数说明:4代表取最大值,6代表忽略错误值
  • MAX+IFERROR组合(兼容旧版Excel):
    =MAX(IFERROR(A:A,""))
    注意:旧版Excel需按Ctrl+Shift+Enter作为数组公式确认,新版Excel直接回车即可

二、VBA代码方案

写法1:遍历单元格筛选日期取最大值

Sub GetLatestDate()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim latestDate As Date
    
    Set ws = ActiveSheet ' 可替换为指定工作表,比如Sheets("日志表")
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' 获取A列最后一行
    latestDate = 0 ' 初始化日期变量
    
    For i = 1 To lastRow
        ' 判断单元格为有效日期且非空
        If IsDate(ws.Cells(i, "A").Value) And ws.Cells(i, "A").Value <> "" Then
            If ws.Cells(i, "A").Value > latestDate Then
                latestDate = ws.Cells(i, "A").Value
            End If
        End If
    Next i
    
    ' 将结果输出到B1,可自行修改输出位置
    ws.Range("B1").Value = latestDate
    ws.Range("B1").NumberFormat = "yyyy-mm-dd" ' 设置日期显示格式
End Sub

写法2:调用Excel内置函数计算(简洁版)

Sub GetLatestDate_Func()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    ' 直接调用MAX函数忽略空白单元格
    ws.Range("B1").Value = Application.WorksheetFunction.Max(ws.Range("A:A"))
    ws.Range("B1").NumberFormat = "yyyy-mm-dd"
End Sub

若列中存在非日期值导致报错,可替换为Application.Aggregate(4,6,ws.Range("A:A"))替代Max方法

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 19:50:55