我的日期栏看起来像这样:
04-03-2013 01:57:32 PM IST
我想把它改成这样:
04/03/2013 13:57:32
我的代码:
Columns("D:E").Select
Selection.Replace What:=" IST", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:=" IDT", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="-", Replacement:="/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
问题是,它改变它看起来像那样:
04-03-2013 01:57:32 PM
并且只有在" PM"它改为:
04/03/2013 13:57:32
我该如何自动完成?
答案 0 :(得分:2)
我认为你只需要指定所需的数字格式:
Selection.NumberFormat = "dd/mm/yyyy hh:mm:ss"
' or mm/dd/yyyy, not sure which you want!
答案 1 :(得分:0)
如果您想将原始日期转换为格式如上的“真实”Excel日期,请尝试在常规模块中:
Option Explicit
Sub ChangeDateTimeFormat()
Dim Rng As Range
Dim C As Range
Set Rng = Range("D:E").SpecialCells(xlCellTypeConstants)
For Each C In Rng
With C
If Right(.Text, 4) = " IDT" Or Right(.Text, 4) = " IST" Then
.Value = DateValue(Left(.Text, Len(.Text) - 4)) + TimeValue(Left(.Text, Len(.Text) - 4))
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End If
End With
Next C
End Sub
仅测试了MDY格式的日期。如果你的日期是DMY格式,这不起作用,我会进一步测试。