[英]Excel VBA - Adding username to now() Timestamp ruins formatting?
[英]Excel VBA timestamp and username
下面的代碼在輸入到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
您可以只使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.