Excel VBA将日期与时间转换

时间:2018-11-13 09:39:49

标签: excel vba excel-vba

我有2个带有时间数据的单元格,其格式如下:

"A1" = Sep 01 2018 00:01:33.707 
"A2" = Sep 01 2018 00:01:49.917

我需要在excel VBA中创建一个按钮和方法,如果时间“ A2”比“ A1”长90秒,它将“ A3”单元格设置为true。 这是我到目前为止所拥有的,但是不起作用:

Sub Macro2()
    Dim str1 As String, str2 As String

    With Worksheets("sheet5")

        str1 = .Cells(1, "A").Text
        str2 = .Cells(2, "A").Text

        'greater than 90m seconds in A3
        .Cells(3, "A") = CBool(Abs((DateValue(Left(str1, 6) & "," & Mid(str1, 7, 5)) + _
                                    TimeValue(Mid(str1, 13, 8)) + TimeSerial(0, 0, 1) * CDbl(Right(str1, 4))) - _
                                   (DateValue(Left(str2, 6) & "," & Mid(str2, 7, 5)) + _
                                   TimeValue(Mid(str2, 13, 8)) + TimeSerial(0, 0, 1) * CDbl(Right(str2, 4)))) > _
                                   TimeSerial(0, 0, 90))
        'actual absolute difference in A4
        .Cells(4, "A") = Abs((DateValue(Left(str1, 6) & "," & Mid(str1, 7, 5)) + _
                                    TimeValue(Mid(str1, 13, 8)) + TimeSerial(0, 0, 1) * CDbl(Right(str1, 4))) - _
                                   (DateValue(Left(str2, 6) & "," & Mid(str2, 7, 5)) + _
                                    TimeValue(Mid(str2, 13, 8)) + TimeSerial(0, 0, 1) * CDbl(Right(str2, 4))))
    End With End Sub

由于Date函数与系统区域设置(在我的情况下为希伯来语,而数据为英语)一起使用,因此上述操作产生了错误。

另一种可能有用的方法是将所有“ A”列(包含日期)转换为系统本地日期,该日期可以与VBA上的日期和时间函数一起使用(不知道该怎么做)。

请帮助

4 个答案:

答案 0 :(得分:1)

我已将您的任务分为3个功能。

a)辅助函数将月份的3个字符转换为整数。看起来有些笨拙,可能还有其他方法,但是使用较大的Select Case的优点是,如果出现其他语言的月份名称,则易于理解和适应:

Function getMonthFromName(monthName As String) As Integer

    Select Case UCase(monthName)
        Case "JAN": getMonthFromName = 1
        Case "FEB": getMonthFromName = 2
        Case "MAR": getMonthFromName = 3
        Case "APR": getMonthFromName = 4
        (...)
        Case "SEP": getMonthFromName = 9
        (...)
    End Select

End Function

b)将字符串转换为日期的函数。它采用您提供的表格中的日期格式,但是如果格式更改,则很容易进行调整(为简单起见,四舍五入秒数)

Function GetDateFromString(dt As String) As Date

    Dim tokens() As String
    tokens = Split(Replace(dt, ":", " "), " ")

    Dim day As Integer, month As Integer, year As Integer
    month = getMonthFromName(CStr(tokens(0)))
    day = Val(tokens(1))
    year = Val(tokens(2))

    Dim hour As Integer, minute As Integer, second As Double
    hour = Val(tokens(3))
    minute = Val(tokens(4))
    second = Round(Val(tokens(5)), 0)

    GetDateFromString = DateSerial(year, month, day) + TimeSerial(hour, minute, second)
End Function

c)一个函数,以秒为单位计算两个日期的差。 VBA(和许多其他环境)中的日期存储为Double,其中Date-Part是整数部分,而date是余数。这使得使用“日期”值进行计算变得容易。

Function DateDiffInSeconds(d1 As String, d2 As String) As Long
    Dim diff As Double
    diff = GetDateFromString(d2) - GetDateFromString(d1)
    DateDiffInSeconds = diff * 24 * 60 * 60
End Function

更新以毫秒为单位:更改GetDateFromString功能。在这种情况下,DateDiffInSeconds应该返回double而不是long

Function GetDateFromString(dt As String) As Date

    Const MillSecPerHour As Long = 24& * 60 * 60 * 1000

    Dim tokens() As String
    tokens = Split(Replace(Replace(dt, ".", " "), ":", " "), " ")

    Dim day As Integer, month As Integer, year As Integer
    month = getMonthFromName(CStr(tokens(0)))
    day = Val(tokens(1))
    year = Val(tokens(2))

    Dim hour As Integer, minute As Integer, second As Integer, milli As Integer
    hour = Val(tokens(3))
    minute = Val(tokens(4))
    second = Val(tokens(5))
    milli = Val(tokens(6))

    GetDateFromString = DateSerial(year, month, day) _
                      + TimeSerial(hour, minute, second) _
                      + milli / MillSecPerHour
End Function

答案 1 :(得分:0)

我认为您过于复杂,请尝试以下操作以了解操作方法:

Sub Macro2()
    Dim str1 As String, str2 As String

    With Worksheets("sheet5")

        .Range("b1:e1") = Split(Range("A1"), " ")
        .Range("B2:e2") = Split(Range("A2"), " ")


End Sub

答案 2 :(得分:0)

为您提供信息,我以略有不同(且更简单)的方式完成了您的工作:

在单元格B2中,我输入了值13/11/2018 11:44:00
在单元格B3中,我输入了值13/11/2018 11:45:01
(对于两个单元格,单元格格式均已设置为d/mm/jjjj u:mm:ss。)

在另一个单元格中,我输入以下公式:

=IF((B3-B2)*86400>90;TRUE;FALSE)

该公式基于设置日期时间值的想法,基于一天等于1,一天中有86400秒的想法。

像这样,您无需VBA就可以计算时差。

答案 3 :(得分:0)

使用UDF。

Sub Macro2()
    Dim str1 As String, str2 As String
    Dim mySecond As Double, t1 As Double, t2 As Double, t3 As Double
    mySecond = TimeSerial(0, 0, 90)

    With Worksheets("sheet5")
        str1 = .Cells(1, "A").Text
        str2 = .Cells(2, "A").Text
        t1 = ConvertTime(str1)
        t2 = ConvertTime(str2)
        t3 = t2 - t1
        .Cells(3, "a") = Abs(t3) >= mySecond
    End With

End Sub
Function ConvertTime(s As String)
    Dim vS
    vS = Split(s, " ")
    ConvertTime = DateValue(vS(0) & "-" & vS(1) & "-" & vS(2)) + TimeValue(Split(vS(3), ".")(0))
End Function