|
本帖最后由 loquat 于 2014-9-23 15:50 编辑
代码整体思路不是很清晰,在VBA中完全可以优化更通用,欢迎拍砖- Sub 替换日期_MeThee()
- '1.将日期统一为8为数字格式
- '2.目前仅支持:“四位年份+分隔符+(1-2位月份)分隔符+(1-2位日期)”格式
- '3.分隔符列表可以在代码中修改
- Dim aRange As Range, aText As String
- Dim 分隔符 As String
- 分隔符 = "[.\-/\\]"
- Set aRange = ActiveDocument.Content
- With aRange.Find
- .ClearFormatting
- .MatchWildcards = True
- .Text = "([0-9]{4})" & 分隔符 & "([0-9]{1,2})" & 分隔符 & "([0-9]{1,2})"
- .Forward = True
- .Wrap = wdFindContinue
- .MatchByte = True
- Do While .Execute = True
- aText = aRange.Text
- With CreateObject("Vbscript.RegExp")
- .Global = True
- .Pattern = "[^0-9]"
- aText = .Replace(aText, "-")
- End With
- aText = Format(CDate(aText), "yyyymmdd")
- aRange.Text = aText
- Loop
- End With
- Set aRange = Nothing
- End Sub
复制代码 |
|