【问题标题】:Convert "Wed Dec 02 24:00:00 GMT 2020" to 02/12/2020 with VBA使用 VBA 将“2020 年 12 月 2 日星期三 24:00:00 GMT 2020”转换为 2020 年 2 月 12 日
【发布时间】: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

标签: excel vba date


【解决方案1】:

这将起作用:

Get-date = CDate(Right(CellRef, 4) & Mid(CellRef, 4, 7))

【讨论】:

    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 2020-06-05
    • 2022-07-20
    • 2021-07-10
    • 2021-03-30
    • 2020-09-30
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多