简体   繁体   English

Excel-VBA现在价值:拆分秒

[英]Excel-VBA Now Value: Split Second

Please can someone assist. 请有人帮忙。 I've searched for this, but have not found anything suitable. 我搜索过这个,但没找到合适的东西。 I have a VBA macro which, when a character is entered in one cell, it places the current computer time in the cell adjacent. 我有一个VBA宏,当在一个单元格中输入一个字符时,它会将当前计算机的时间放在单元格中。 I've designed a timing sheet for races, and need to get the time a racer completes the race. 我为比赛设计了一张计时表,并且需要让赛车手完成比赛。 The challenge is that I'm using the 挑战在于我正在使用

.Value = Now .Value =现在

command, and although I've also used the 命令,虽然我也用过

.NumberFormat = "dd/mm/yyyy hh:mm:ss.00" .NumberFormat =“dd / mm / yyyy hh:mm:ss.00”

command, I don't get the split seconds. 命令,我没有得到分秒。 It will return a value of 它将返回值

03/10/2017 13:14:27.00 2017/03/10 13:14:27:00

The split seconds are always 0. 分秒始终为0。

If I simply use the NOW() formula in Excel and change the number format to include split seconds, the computer time inherently contains split seconds and it displays perfectly there, but this is does not work when I use the macro. 如果我只是在Excel中使用NOW()公式并将数字格式更改为包括分秒,计算机时间本身就包含分秒,并且它在那里显示完美,但是当我使用宏时这不起作用。

Can anyone assist me in getting the time down to split-second level? 任何人都可以协助我把时间缩短到第二级吗? My full code for the macro below: 我的宏的完整代码如下:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim rCell As Range
Dim rChange As Range

On Error GoTo ErrHandler
Set rChange = Intersect(Target, Range("B2:B1000000"))
If Not rChange Is Nothing Then
    Application.EnableEvents = False
    For Each rCell In rChange
        If rCell > "" Then
            With rCell.Offset(0, 1)
                .Value = Now
                .NumberFormat = "dd/mm/yyyy hh:mm:ss.00"
            End With
        End If
    Next
End If
ExitHandler:
  Set rCell = Nothing
  Set rChange = Nothing
  Application.EnableEvents = True
  Exit Sub
ErrHandler:
  MsgBox Err.Description
  Resume ExitHandler
End Sub

您可以像这样调用工作表函数

.Value = [Now()]

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

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