Excel VBA时间戳和用户名

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

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 

你可以使用像date & " " & time 。 这将输出date和时间连接如下:

 17/07/2013 11:49:39 PM 

这里是你的代码join到下一列的date/时间值:

 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