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 小时前
从Excel姓名匹配案例学Python:由点及面的系统化学习指南
开发语言·python·excel
best_scenery3 小时前
用excel绘制茎叶图
excel·分布图
rannn_11119 小时前
【学以致用|python自动化办公】OCR批量识别自动存为Excel(批量识别发票)
python·ocr·excel·财务
m0_64880493_江哥20 小时前
用正则方法从中英文本提取英文的python示例
python·mysql·正则表达式
九皇叔叔21 小时前
Linux Shell 正则表达式:从入门到实战,玩转文本匹配与处理
linux·mysql·正则表达式
小钱c71 天前
Python使用 pandas操作Excel文件并新增列数据
python·excel·pandas
Shi_haoliu1 天前
Vue2 + Office Add-in关于用vue项目于加载项控制excel单元格内容(Demo版)
前端·javascript·vue.js·node.js·html·excel·office
njsgcs1 天前
json转excel python pd
python·json·excel·pd
RECRUITGUY1 天前
Excel中将毫秒时间戳转换为标准时间格式
excel
SunkingYang1 天前
详细介绍C++中捕获异常类型的方式有哪些,分别用于哪些情形,哪些异常捕获可用于通过OLE操作excel异常
c++·excel·mfc·异常捕获·comerror