【发布时间】:2021-01-18 10:44:11
【问题描述】:
我想使用 VBA 将“Wed Dec 02 24:00:00 GMT 2020”转换为“02/12/2020”。
我创建了一个自定义函数。
Function Get_date(CellRef As String)
Dim Year_num As Integer, Month_name As String, Month_num As Integer, Date_num As Integer'''
Year_num = Right(CellRef, 4)
Month_name = Mid(CellRef, 5, 3)
Month_num = Month(DateValue(Month_name&("1")))
Date_num = Mid(CellRef, 9, 2)
Get_date = DateSerial(Year_num, Month_num, Date_num)
End Function
【问题讨论】:
-
究竟是什么不起作用?你得到什么输出?
-
如果 24:00:00 是有效时间,我会感到惊讶:不是第二天午夜吗?这不应该是 2020 年 3 月 12 日而不是 12 年 2 月吗?
-
如果您有英文月份,您的函数将使用此更正:
Month_num = Month(DateValue(Month_name & "-1"))。阅读here,了解为什么使用Long而不是Integer。