如果行已更改,请在Excel工作表中添加时间戳

如果行已更改,我需要添加或更新时间戳,以优化工作簿。 我正在做数据导入,但我需要看哪一行更新/添加和哪个date。

到目前为止,我已经find并调整了以下代码:

Private Sub Worksheet_Change(ByVal Target As Excel.Range) With Target If .Count > 1 Then Exit Sub If Not Intersect(Range("A2:BL9999"), .Cells) Is Nothing Then Application.EnableEvents = False With .Cells(1, 65) .NumberFormat = "yyyy.mm.dd" .Value = Now End With Application.EnableEvents = True End If End With End Sub 

问题是,时间戳总是相对于已经做出改变的行+ 65行而不是列BM(索引65)。

你能告诉我,我应该使用或更改哪个函数?

以及列BM更好地修复

  1. 处理所有可能已经改变的行,而不是退出并输出任何logging
  2. closuresScreenUpdating以获得速度

 Private Sub Worksheet_Change(ByVal Target As Range) Dim rng1 As Range Dim rng2 As Range Set rng1 = Intersect(Target, Range("A2:BL9999")) If rng1 Is Nothing Then Exit Sub With Application .ScreenUpdating = False .EnableEvents = False End With For Each rng2 In rng1.Cells With Cells(rng2.Row, 65) .NumberFormat = "yyyy.mm.dd" .Value = Now End With Next With Application .ScreenUpdating = True .EnableEvents = True End With End Sub 

要相对于整行更改引用,请使用.EntireRow

所以这一行应该是: With .EntireRow.Cells(1, 65)

请注意,即使使用单个行,仍可以使用A1样式引用。 这可以防止你必须计算列。 例如,在这种情况下, .EntireRow.Range("BM1")含义与.EntireRow.Range("BM1")完全相同。