你可以尝试一下。这使用正则表达式(常用表达)检查您的日期。
您需要设置对Microsoft VBScript 正则表达式 x.x
Sub ChangeDates()
Dim RegEx As New RegExp, rng As Range, i As Long, s As String
Dim tempArr() As String, bFlag As Boolean
With RegEx
.Pattern = "(\d{2})/(\d{2})/(\d{4})"
For Each rng In ActiveSheet.UsedRange
tempArr = Split(rng.Text)
bFlag = False
For i = 0 To UBound(tempArr)
If .test(tempArr(i)) Then
s = tempArr(i)
'Subtract 1 year from original date
s = Format(DateAdd("YYYY", -1, CDate(s)), "MM/DD/YYYY")
'Swap month and day field
tempArr(i) = Format(DateSerial(.Replace(s, "$3"), _
.Replace(s, "$2"), .Replace(s, "$1")), "mm/dd/yyyy")
'Tell VBA that the string has change and to update sheet
bFlag = True
End If
Next
If bFlag = True Then rng.Value = Join(tempArr)
Next rng
End With
End Sub
分解正则表达式模式:(\d{2})/(\d{2})/(\d{4})
该表达式分为三组:(\d{2})
, (\d{2})
, (\d{4})
第 1 组和第 2 组找到任意两个({2}
) digits (\d
),后跟一个正斜杠/
第 3 组正在寻找任意四个({4}
) digits (\d
)在正斜杠之后/