【问题标题】:Change time format from number like 600 to 06:00:00将时间格式从 600 等数字更改为 06:00:00
【发布时间】:2019-05-23 09:30:45
【问题描述】:

我有两个工作簿,A 和 B,以及两个工作簿中的两个工作表,1 和 2。我需要根据时间比较并找到匹配项。在工作表 1 的工作簿 B 中,我有 hh:mm:ss 24 小时时间格式。但在工作簿 A 中,我的时间以数字格式显示,如 600 和 800 等。我已将工作簿 1 中的时间格式更改为 hh:mm:ss 但第一次运行后遇到的问题time 是时间已更改为 06:00:00,在第二次运行时再次更改为 00:00:00。

    Dim rCell As Range
    Dim iHours As Integer
    Dim iMins As Integer
    Dim lrow As Long
    Dim rn As Range

    lrow = Sot.Range("d" & Rows.Count).End(xlUp).Row
    Sot.Activate
    Set rCell = Sot.Range(Cells(5, 4), Cells(lrow, 5))

    For Each rn In rCell
        If IsNumeric(rn.Value) And Len(rn.Value) > 0 Then
            iHours = rn.Value \ 100
            iMins = rn.Value Mod 100
            rn.Value = (iHours + iMins / 60) / 24
            rn.NumberFormat = "h:mm:ss"
        End If
    Next

    For i = 5 To eRowplan
        Time_from = Sotplan.Range("D" & i).Value
        Time_To = Sotplan.Range("E" & i).Value
        Time_from = TimeSerial(Hour(Time_from), Minute(Time_from), 
        Second(Time_from))
        Time_To = TimeSerial(Hour(Time_To), Minute(Time_To), Second(Time_To))
        If B_Time > "24:00" Then B_Time = "23:59"
        B_Time = TimeSerial(Hour(B_Time), Minute(B_Time), Second(B_Time))

我试过上面的代码

【问题讨论】:

  • 什么是600和800?秒?分钟?小时?
  • 那个 600 是 06:00:00 hh:mm:ss
  • 如果 600 表示 06:00:00,我猜 800 表示 08:00:00。在这种情况下,只需将值除以 100,然后再除以 24。如果您应用时间格式,600/100/24 之类的将返回一个与 06:00:00 等效的十进制值。 800/100/24 将返回 08:00:00
  • 你如何表示秒? 8hrs 10 sec80010 还是 800.1

标签: excel vba


【解决方案1】:

如果您想要VBA 解决方案,例如,您可以使用以下方法进行操作。 600 = 600 分钟

Dim c

For Each c In Selection
    c.Value2 = c / (24 * 60)
    c.NumberFormat = "hh:mm"
Next c

或者只使用= A1 / (24 * 60) 并格式化为Time


cmets 后更新

对于 600 = 06:00:00,您可以使用以下内容

Dim c, tmpTime As Variant

For Each c In Selection
    ' Test if number
    If IsNumeric(c.Value2) Then
        ' Split into character array, the Len(c.Value2) limits the size of the array otherwise
        ' an additional empty element is created
        tmpTime = Split(StrConv(c.Value2, vbUnicode), Chr$(0), Len(c.Value2))
        ' Write results back and format
        With c.Offset(0, 1)
            .Value2 = Join(tmpTime, ":")
            .NumberFormat = "hh:mm:ss"
        End With
    End If
Next c

【讨论】:

  • 但是600/1440 不等于10 小时吗?
  • @RonRosenfeld 是的 - 我把这个问题读成 600 是分钟数。
  • 所有这一切的困难之处在于,我们得到的数据不完整,关于时间如何以非标准格式表示。例如。 1200 ?= 12 小时。秒是如何表示的?
  • @RonRosenfeld 同意 - 这有限制
猜你喜欢
  • 2012-04-30
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多