1

下面的代码在输入到 A 列时检测数据,并自动将当前用户插入到右侧的单元格中。我也希望这段代码也添加一个时间戳。我需要记录用户名和时间。有什么建议么?

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("A:A"))
    If Not rChange Is Nothing Then
        Application.EnableEvents = False
        For Each rCell In rChange
            If rCell > "" Then
                With rCell.Offset(0, 1)
                    .Value = UserName()

                End With

            Else
                rCell.Offset(0, 1).Clear
            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

Public Function UserName()
    UserName = Environ$("UserName")
End Function
4

1 回答 1

1

你可以使用类似的东西date & " " & time。这将输出连接的日期和时间,如下所示:

17/07/2013 11:49:39 PM

这是您的代码,其中将日期/时间值添加到下一列中:

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("A:A"))
If Not rChange Is Nothing Then
    Application.EnableEvents = False
    For Each rCell In rChange
        If rCell > "" Then
            rCell.Offset(0, 1).Value = UserName()   
            rCell.Offset(0, 2).Value = date() & " " & time()   <-- added line          
        Else
            rCell.Offset(0, 1).Clear
        End If
    Next
End If
于 2013-07-17T14:20:05.737 回答