我之前提出了一个问题,我一直在使用最后保存的代码来注册每张代码的更改。
进行更改的时间将记录在索引表上,以显示每张表的最后修改时间。
然而,代码记录了我访问工作表的时间并且没有修改,因此使其不准确。
是否有任何其他更有效和准确的解决方案来注册对工作表所做的更改而不是访问?
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal target As Range)
If Sh.Name = "Index" Then Exit Sub
i = Sh.Index
With Sheets("Index")
.Cells(i + 2, 1) = Sh.Name
.Cells(i + 2, 2) = Now
End With
End Sub
这是代码
最佳答案
您的方法不好,因为您使用工作表的索引 ,它表示相对于其他工作表放置工作表的位置/顺序。所以如果您移动工作表,您的代码将覆盖 用于另一张纸的行。
所以我基于我对工作表名称的提议 (这也可以改变,但它会 只添加一个新行而不覆盖现有数据 ),并在第三列中添加了已修改的范围。
试试这个:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "Index" Then Exit Sub
If Target.Cells.Count = 1 And Not Application.Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
Dim AlreadyExist As Boolean, _
LastRow As Integer, _
WsI As Worksheet
Set WsI = ThisWorkbook.Sheets("Index")
With WsI
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
AlreadyExist = False
For i = 1 To LastRow
'Look for the good row to update
If .Cells(i, 1) <> Sh.Name Then
Else
AlreadyExist = True
.Cells(i, 2) = Now
.Cells(i, 3) = Target.Address(False, False, xlA1)
End If
Next i
'If the sheet didn't exist, add a new line for it
If AlreadyExist Then
Else
.Cells(LastRow + 1, 1) = Sh.Name
.Cells(LastRow + 1, 2) = Now
.Cells(LastRow + 1, 3) = Target.Address(False, False, xlA1)
End If
End With
End Sub
关于vba - 在工作簿的任何工作表中保存最后更改/修改的代码不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33209199/