简体   繁体   English

如何在Excel中显示hh:mm:ss?

[英]How to display hh:mm:ss in Excel?

I'm new to Excel VBA. 我是Excel VBA的新手。 I want to set up a loop that will display the time for each minute on the minute in a new cell. 我想设置一个循环,显示新单元格中每分钟的时间。

Is there a way to display only the time (hh:mm:ss) in a cell without seeing the date beside it? 有没有办法在单元格中只显示时间(hh:mm:ss)而不会看到它旁边的日期? Also, I'd like the time zone shown. 另外,我想要显示的时区。 (eg Singapore Time, "SGT"). (例如新加坡时间,“SGT”)。

Here is an example of the output I'd like to see: 以下是我想看到的输出示例:

23-Oct-13 10:00:00 AM SGT
23-Oct-13 10:01:00 AM SGT
23-Oct-13 10:02:00 AM SGT
23-Oct-13 10:03:00 AM SGT
23-Oct-13 10:04:00 AM SGT
23-Oct-13 10:05:00 AM SGT
23-Oct-13 10:06:00 AM SGT
23-Oct-13 10:07:00 AM SGT
23-Oct-13 10:08:00 AM SGT
23-Oct-13 10:09:00 AM SGT
23-Oct-13 10:10:00 AM SGT
23-Oct-13 10:11:00 AM SGT
23-Oct-13 10:12:00 AM SGT
23-Oct-13 10:13:00 AM SGT
23-Oct-13 10:14:00 AM SGT
23-Oct-13 10:15:00 AM SGT
23-Oct-13 10:16:00 AM SGT
23-Oct-13 10:17:00 AM SGT
23-Oct-13 10:18:00 AM SGT
23-Oct-13 10:19:00 AM SGT
23-Oct-13 10:20:00 AM SGT
...

This Should do it, have Fun. 这应该做到,玩得开心。 :-) :-)

Option Explicit
Sub test()
    Dim i&, sTime As Date, TimeStep As Date, Cell As Range
    '// Format range cell to 24Hrs
    With Range("A1:A1440") '// 24H*60M
        .NumberFormat = "HH:MM:SS;@"
    End With

    Set Cell = [A1] '// first cell to write
        sTime = "00:00:00"  '// start time
    '// Loop 24hrs
    TimeStep = "00:01:00" '// time increment
    For i = 0 To 1439 ' // 1439 means 1439 cells
        Cell.Offset(i, 0).Value = sTime
        sTime = sTime + TimeStep
    Next i
End Sub

Start 开始

在此输入图像描述

End 结束

在此输入图像描述

ok, now that you explained a bit in more depth, 好的,既然你有更深入的解释,

ActiveSheet.Cells(1, 1).Value = "00:00:00"
ActiveSheet.Cells(2, 1).Value = "00:01:00"
Range("A1:A2").Select
Selection.AutoFill Destination:=Range("A1:A1440"), Type:=xlFillDefault

should do the trick 应该做的伎俩

To retrieve time format you can use: 要检索时间格式,您可以使用:

=Mid(A1,11,8)*1 into function bar (with additional column)

or into a sub 或者成子

timeOnClock = Mid(MyString, 10, 9)*1

then for your first row (23-Oct-13 10:00:00 AM SGT) you retrieve 0,416666667 and you can choice your prefer format 那么对于你的第一行(23-Oct-13 10:00:00 AM SGT) you retrieve 0,416666667 ,你可以选择你喜欢的格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM