我想区分清除单元格与更改单元格
当更改单元格时,我需要帮助做一个VBA,以在同一行中添加时间戳。
这就是我所做的。
在单元格中输入一个值,下一个单元格被“ Enter”激活。我用偏移量在同一行中添加时间戳。
但是问题是,当删除单元格内容时,相同的单元格是有效的,因此时间戳在上面的一行中更新,而不是同一行。
您能帮我解决这个问题吗?
先感谢您。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rDependents As Range
On Error Resume Next
Set rDependents = Target.Dependents
If Err.Number > 0 Then
Exit Sub
End If
If Not Application.Intersect(rDependents, Range("G9:G500")) Is Nothing Then
Call Time_Stamp
End If
End Sub
Private Sub Time_Stamp()
ActiveCell.Offset(rowOffset:=-1, columnOffset:=2).Activate
With Selection
.Value = Now
End With
ActiveCell.Offset(rowOffset:=1, columnOffset:=-3).Activate
End Sub
I need help to make a VBA to add the time stamp in the same row when a cell is changed.
This is what I made.
A value is entered in a cell, the next cell is activated by "enter". I used offset to add the time stamp in the same row.
But the issue is when the cell content is deleted, the same cell is active, so the time stamp is updated in one row above, not the same row.
Could you please help me to solve this issue?
Thank you in advance.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rDependents As Range
On Error Resume Next
Set rDependents = Target.Dependents
If Err.Number > 0 Then
Exit Sub
End If
If Not Application.Intersect(rDependents, Range("G9:G500")) Is Nothing Then
Call Time_Stamp
End If
End Sub
Private Sub Time_Stamp()
ActiveCell.Offset(rowOffset:=-1, columnOffset:=2).Activate
With Selection
.Value = Now
End With
ActiveCell.Offset(rowOffset:=1, columnOffset:=-3).Activate
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用Worksheet_change事件时,更改ActiveCell并不是一个好主意,因为ActiveCell可能是任何事物,具体取决于用户如何进入值。
您不知道活动单元将是什么,因此请使用
activecell.offset(rowoffset:= - 1
可以最终到达任何地方。对象
。设置更改导致实际值,而不是空白单元格
。
It's not a good idea to change the ActiveCell when using the Worksheet_Change event, because the ActiveCell could be anything, depending how the user enters the value.
You cannot know what the active cell will be, so using
ActiveCell.Offset(rowOffset:=-1
could end up anywhere.Instead of the active cell, operate on the
target
object. You don't need to call another sub for that, you can do it right in the Change eventNow you don't need to muck around with offset to get the row where the change happened, and the time stamp will only be set if the change resulted in a real value, not a blank cell.
If, for some reason you want to use a separate sub, just feed it the row number of Target as a parameter.