VBA - 日期时间字符串 Replace/Substitute
VBA - Date-Time String Replace/Substitute
我有一列包含来自 Outlook 的电子邮件的日期和时间。某些日期的格式为 - January 2, 2020 4:15 PM
、January-14-20 12:44 PM
、December-24-19 20:15 PM
。
我已尝试使用 Replace 和 Substitute 函数,Replace 确实按定义工作,但从字符串中删除了时间。我希望所有日期都为 2019-12-27 3:02 PM
。
sub Replace()
Dim sString, searchString as String
dim i, LastCell as Long
LastCell = Range("C" & Rows.Count).End(xlUp).Row
Set searchRng = Range("C3:C" & lastCell)
For Each c In searchRng
sString = c
searchString = "-"
If InStr(1, sString, searchString, vbTextCompare) > 0 Then
i = InStr(1, sString, SearchString, vbTextCompare)
i = InStr(i + 1, sString, SearchString, vbTextCompare)
c.Offset(0, 1) = WorksheetFunction.Replace(sString, i, "19", " 2020")
End If
Next c
End Sub
CDate
将 date/time 字符串转换为真正的 date/time 可以格式化。
所以代码如下:
For Each c In rngSrc
c.Offset(0, 1) = CDate(c)
c.Offset(0, 1).NumberFormat = "yyyy-mm-dd h:mm AM/PM"
Next c
将转换
我仍然认为你可以更轻松地做到这一点:
'
'
'
LastCell = Range("C" & Rows.Count).End(xlUp).Row
With Range("C3:C" & lastCell).Offset(0, 1)
.FormulaR1C1 = "=TEXT(RC[-1],""yyyy-mm-dd hh:mm AM/PM"")"
.Value = .Value
End With
注意: yyyy-mm-dd hh:mm AM/PM
将根据您的区域设置工作,因此如果 Excel 不是英文,请翻译它。我的 excel 是西班牙语,除非我输入 aaaa-mm-dd hh:mm AM/PM
而不是 yyyy-mm-dd hh:mm AM/PM
,否则此代码对我不起作用
一次转换所有日期。
范围
不让 Excel 猜测的真正安全的方法:
使用 Regular Expression to parse and split the date into pieces (eg using this pattern):
然后使用DateSerial function and TimeSerial function创建一个真实的日期值:
Dim RealDateTime As Date
RealDateTime = DateSerial(2020, 2, 20) + TimeSerial(16, 50, 22)
可以将其作为实际日期值写入单元格:
Range("A1").Value = RealDateTime
并按照您喜欢的方式进行格式化(因为它是一个真实的日期值)
Range("A1").NumberFormat = "yyyy-mm-dd hh:mm AM/PM"
我有一列包含来自 Outlook 的电子邮件的日期和时间。某些日期的格式为 - January 2, 2020 4:15 PM
、January-14-20 12:44 PM
、December-24-19 20:15 PM
。
我已尝试使用 Replace 和 Substitute 函数,Replace 确实按定义工作,但从字符串中删除了时间。我希望所有日期都为 2019-12-27 3:02 PM
。
sub Replace()
Dim sString, searchString as String
dim i, LastCell as Long
LastCell = Range("C" & Rows.Count).End(xlUp).Row
Set searchRng = Range("C3:C" & lastCell)
For Each c In searchRng
sString = c
searchString = "-"
If InStr(1, sString, searchString, vbTextCompare) > 0 Then
i = InStr(1, sString, SearchString, vbTextCompare)
i = InStr(i + 1, sString, SearchString, vbTextCompare)
c.Offset(0, 1) = WorksheetFunction.Replace(sString, i, "19", " 2020")
End If
Next c
End Sub
CDate
将 date/time 字符串转换为真正的 date/time 可以格式化。
所以代码如下:
For Each c In rngSrc
c.Offset(0, 1) = CDate(c)
c.Offset(0, 1).NumberFormat = "yyyy-mm-dd h:mm AM/PM"
Next c
将转换
我仍然认为你可以更轻松地做到这一点:
'
'
'
LastCell = Range("C" & Rows.Count).End(xlUp).Row
With Range("C3:C" & lastCell).Offset(0, 1)
.FormulaR1C1 = "=TEXT(RC[-1],""yyyy-mm-dd hh:mm AM/PM"")"
.Value = .Value
End With
注意: yyyy-mm-dd hh:mm AM/PM
将根据您的区域设置工作,因此如果 Excel 不是英文,请翻译它。我的 excel 是西班牙语,除非我输入 aaaa-mm-dd hh:mm AM/PM
而不是 yyyy-mm-dd hh:mm AM/PM
一次转换所有日期。
范围
不让 Excel 猜测的真正安全的方法:
使用 Regular Expression to parse and split the date into pieces (eg using this pattern):
然后使用DateSerial function and TimeSerial function创建一个真实的日期值:
Dim RealDateTime As Date
RealDateTime = DateSerial(2020, 2, 20) + TimeSerial(16, 50, 22)
可以将其作为实际日期值写入单元格:
Range("A1").Value = RealDateTime
并按照您喜欢的方式进行格式化(因为它是一个真实的日期值)
Range("A1").NumberFormat = "yyyy-mm-dd hh:mm AM/PM"