简体   繁体   中英

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. 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

command, and although I've also used the

.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

The split seconds are always 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.

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()]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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