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
相关推荐
骆驼爱记录1 天前
Word题注编号间距调整4种方法
自动化·word·excel·wps·新人首发
Eiceblue1 天前
两种实用方法删除 Excel 重复行:Python + VBA
开发语言·python·excel
m5655bj2 天前
通过 Python 将 Excel 转换为 TXT文本
开发语言·python·excel
幼稚园的山代王2 天前
Java 正则核心 API 拆解
java·开发语言·正则表达式
马猴烧酒.2 天前
【正则表达式详解|Java】从0学习实战够用
学习·正则表达式
要做一个小太阳2 天前
excel VLOOKUP函数
excel
博风2 天前
用excel写一个年会抽奖的小程序
excel
LAM LAB2 天前
【VBA/ppt】设置选中字体为红色
powerpoint·vba·wps
零零发聊技术2 天前
Excel实现数字与列标之间的转换
excel·vba·office
老吴学AI2 天前
第 3 节 | 视觉魔术:Excel 如何用颜色和位置“控制”读者视线
信息可视化·数据分析·excel·职场·数据可视化·报表制作·用数据讲故事