我有下面的代码,它将依赖于触发器,列A和B,以指示每个已完成条目的时间戳,但是,我继续获得运行时错误1004弹出窗口:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A2:B501")) Is Nothing Then
Target.Offset(0, 12) = Now
Target.Offset(1, 13) = Now
Target.Offset(0, 14) = Environ("UserName")
End If
End Sub
我是VBA的新手,希望你们能帮助我。
答案 0 :(得分:1)
处理此问题的更好方法是:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Application.Intersect(Target, Me.Range("A2:B501"))
'Work with "rng", not "Target", since Target might contain cells
' outside of ColA or B...
If Not rng Is Nothing Then
'Target (and so rng) could contain multiple cells
' so need to address them individually
For Each c In rng.Cells
'EDIT:
c.Offset(0, 12) = Now 'ColA >> M, ColB >> N
c.EntireRow.Cells(1, "O") = Environ("UserName")
Next c
End If
End Sub
但是,由于您同时处理ColA和ColB,您可能最终会覆盖值,因为从A和B偏移的范围将重叠