我正在尝试让我的宏打印出我的数据在H列中更新的日期。另外我还需要它在更新的同一行打印,所以假设我更新A2,我想要日期在H2等打印。
我有这个:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> Range("H" & Target.Row).Address Then
Range("H" & Target.Row).Value = Date
End If
End Sub
出于某种原因,当我最终改变某些东西并且进入时,我得到一个小延迟,但是日期永远不会在H中打印。不确定我在这里做错了什么。
答案 0 :(得分:2)
Private Sub Worksheet_Change(ByVal Target As Range)
Const DATE_COL As Long = 8
Dim c As Range
'don't trigger if user (eg) deletes a whole column!
Debug.Print Target.Cells.Count
If Target.Cells.Count > 1000 Then Exit Sub
On Error GoTo haveError
Application.EnableEvents = False
For Each c In Target.Cells
If c.Column <> DATE_COL Then
c.EntireRow.Cells(DATE_COL).Value = Date
End If
Next c
haveError:
Application.EnableEvents = True
End Sub
答案 1 :(得分:0)
你需要:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> Range("H" & Target.Row).Address Then
Application.EnableEvents = False
Range("H" & Target.Row).Value = Date
Application.EnableEvents = True
End If
End Sub