Excel更改日期格式

标签 excel vba

我提取了一份宏报告,该报告为我提供了以下日期格式:JUL13/2023 有没有一种方法或公式可以将其转换为常规日期格式? 我需要跟踪从这一天到某一天过去了多少天。

最佳答案

尝试这个测试:

Function convert_date(date_as_string As String) As Date
    Dim mthstring As String
    mthstring = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC"
    convert_date = DateSerial( _
    CInt(Right(date_as_string, 4)), _
    CInt(((InStr(1, mthstring, Left(date_as_string, 3)) - 1) / 4) + 1), _
    CInt(Replace(Mid(date_as_string, 4, 2), "/", "")))
End Function

Sub test()
    Debug.Print convert_date("JUL13/2023")
    Debug.Print convert_date("JUL8/2023")
End Sub

Sub test2()
    Dim var As String
    var = Range("A1").Value
    Range("B1").Value = convert_date(var)
End Sub

Sub test3()
    Dim start_row As Long, end_row As Long, sht As Worksheet
    
    Set sht = ActiveSheet
    start_row = 2
    end_row = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    
    For Each cel In sht.Range("B" & start_row & ":B" & end_row).Cells
        cel.Value = convert_date(cel.Offset(0, -1).Value)
    Next
    
End Sub

关于Excel更改日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75134907/

相关文章:

excel - IMEX=1 和 TypeGuessRows=0 不适用于 ZipCode

excel - 在 VBA 中使用 InStr 进行多字符串搜索

vba - 如何比较两个不同工作表上的两个不同列并突出显示差异?

vba - 将文本转换为数字的宏会产生不一致的结果

excel - VBA for Excel : Copy values of one row to another with a unique Identifier

excel vba range.find找不到日期

sql - 如何从 adodb 记录集 Excel VBA 中的一列中选择不同的值?

Excel VBA 等待 Application.CalculateFull

arrays - 如何将 3 维值传递给 vba 并使用它

json - 使用 VBA 将 JSON 解析为 Excel