Excel·VBA日期时间转换提取正则表达式函数

标准日期转换

vbnet 复制代码
Function 标准日期(ByVal str$) As Date
    Dim pat$, result$
    arr = Array("(\d{4}).*?(\d{1,2}).*?(\d{1,2})", "(\d{4}).*?(\d{1}).*?(\d{1,2})")
    If Len(str) < 8 Then pat = arr(1) Else pat = arr(0)
    With CreateObject("vbscript.regexp")  '正则表达式
        .Global = True
        .Pattern = pat
        result = .Replace(str, "$1/$2/$3")
    End With
    标准日期 = Format(result, "yyyy/mm/dd")
End Function

标准日期时间提取

vbnet 复制代码
Function 标准日期时间(ByVal str$) As Date
    Dim pat$, res$, result$, t&
    On Error Resume Next
    pat = "(\d{4}).*?(\d{1,2}).*?(\d{1,2}).*?(\d{1,2}).*?(\d{1,2}).*?(\d{1,2}).*"
    res = "$1/$2/$3 $4:$5:$6"
    With CreateObject("vbscript.regexp")  '正则表达式
        .Global = True
        .Pattern = pat
        Do
            result = .Replace(str, res): t = CLng(Mid(result, 1, 4))
            If CDate(result) = "0:00:00" Or t < 1900 Then str = Mid(str, 2) Else Exit Do
            If Len(str) < 6 Then result = "#Error": Exit Do  '没有日期
        Loop While Len(str) > 1
    End With
    标准日期时间 = Format(result, "yyyy/mm/dd hh:mm:ss")
End Function
相关推荐
wuyk5558 小时前
Python办公自动化-第04讲-批量新建Excel
开发语言·python·excel
ScilogyHunter12 小时前
[14] TMS570LC4357_VIM向量中断:128 通道、双 VIM 与硬件向量分发
单片机·编辑器·vim·excel·hercules·tms570
wuyk5551 天前
Python办公自动化-第02讲-Excel文件认知与四大库选型
开发语言·python·excel
用户298698530142 天前
React 前端实现 Excel 与 HTML 双向转换:基于 WebAssembly 的浏览器端方案
javascript·html·excel
用户0332126663673 天前
使用 Python 轻松移除 Excel 文档中的重复行
python·excel
AI英德西牛仔3 天前
智谱清言导出word指令失效?PC端深度用户正在用“AI导出鸭”重构内容归档流
人工智能·重构·word·excel·deepseek·ai导出鸭
用户0332126663674 天前
使用 Python 删除 Excel 中的空白行或空白列
python·excel
wuyk5557 天前
Spider-第09讲-三格式落盘CSV-Excel-JSON
爬虫·python·json·excel
黎羽_7 天前
Excel 插图在手机端不显示/错位:完整排查手册
excel·openpyxl·移动端·办公文档
用户298698530149 天前
React 前端实现 Excel 与 CSV 互转:基于 WebAssembly 的浏览器端方案
javascript·react.js·excel