尝试使用此自定义格式:
yyyy/mm/dd h:mm;@
要输入它,请右键单击单元格并选择单元格格式:
然后在Number
选项卡下Custom
从列表框中选择。并在文本框中输入提供的格式Type:
。
如果这不起作用。你 ***** 绝对 ***** 肯定 04/06/2013 09:00:00 是 2013 年 4 月 6 日而不是 2013 年 6 月 4 日?
如果这仍然不起作用并且您已验证日期是正确的。
Sub ChangeDateFormat()
Application.ScreenUpdating = False
Dim CurrentCell As Range
Dim LastRow As Long
Dim RegEx As Object
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
LastRow = Range("A" & Rows.Count).End(xlUp).Row 'Get The Last Row in Column Change A as Needed
For Each CurrentCell In Range("A1:A" & LastRow) ' Loop through all cells. Change Column as needed
If InStr(CurrentCell.Value, "/") <> 0 Then 'To make sure only convert non converted ones
RegEx.Pattern = "(\d{2})/(\d{2})/(\d{4}) (\d{2}):(\d{2}):(\d{2})" ' Seperate all parts of imported Data into groups
CurrentCell.Value = RegEx.Replace(CurrentCell.Value, "$3.$2.$1 $4:$5") ' Change order of groups and place into cell.
End If
Next
Application.ScreenUpdating = True
End Sub
*****注意: ***** 这仅在所有日期值都为的情况下才有效,dd/mm/yyyy hh:mm:ss
如果它们不是,您将不得不添加一些错误处理并可能稍微修改代码,因为它会导致问题。
这也适用于其他文本内的日期。如果A1
值为
This is a test 04/06/2013 09:00:00 Lets see what happens
那么新值将是
This is a test 2013.06.04 09:00 Lets see what happens