用于保存工作簿的任何工作表中的上次更改/修改的代码不准确

时间:2015-10-19 07:50:40

标签: excel vba excel-vba events event-handling

我之前提出过一个问题,我一直在使用上次保存的代码来注册每张代码的更改。

更改的时间将在索引表上注册,以显示每张表的最后修改时间。

然而,代码记录了我访问工作表的时间而不是修改因此使其不准确。

是否还有其他更高效,更准确的解决方案来注册对工作表所做的更改而不是访问?

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

这是代码

3 个答案:

答案 0 :(得分:2)

您的方法效果不佳,因为您使用了工作表的索引,它表示相对于其他工作表放置工作表的地点/顺序。所以如果您移动工作表,您的代码将覆盖用于另一个工作表的行。

所以我基于我的建议在工作表名称上(也可以改变,但它只添加新行而不覆盖现有数据),并添加在第三列中修改了范围。

尝试一下:

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

答案 1 :(得分:1)

我使用文件的上次修改日期解决您的问题。它适用于已保存的文件。

 Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal target As Range)

    Dim ws As Worksheet
    Dim wb As Workbook
    Dim MaxRange As Range
    Dim Maxvalue As Double


        Set wb = ThisWorkbook
        Set ws = ThisWorkbook.Sheets("Index")
        Set MaxRange = ws.Columns(2)


        sPath = wb.FullName

        'Debug.Print Sh.Name

      Maxvalue = Application.WorksheetFunction.Max(MaxRange)

        'Debug.Print Format(Maxvalue, "DD/mm/YYYY")

    If Sh.Name = "Index" Then Exit Sub

    ' Find the Last row

    lastrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

    Set fs = CreateObject("Scripting.FileSystemObject")

    Set f = fs.GetFile(sPath)
    filemoddate = CDate(f.DateLastModified)

      ' Debug.Print filemoddate

      ' Debug.Print CDate(ws.Cells(lastrow, 2).Value)

     If filemoddate > CDate(Maxvalue) Then

        With ws.UsedRange
             Set rfound = .Find(Sh.Name, LookIn:=xlValues)
                If Not rfound Is Nothing Then
                     lastrow = rfound.Row

                ' Print if the Modified Date if the file name present

                     ws.Cells(lastrow, 2).Value = filemoddate
                Else

                ' Print if the Modified Date and Sheet Name if the file 'name is not present

                   ws.Cells(lastrow + 1, 1).Value = Sh.Name
                    ws.Cells(lastrow + 1, 2).Value = filemoddate
                 End If
        End With

   End If


   Set f = Nothing
   Set fs = Nothing
   Set ws = Nothing
   Set wb = Nothing
   Set rfound = Nothing

End Sub

答案 2 :(得分:1)

如果这是工作簿中唯一的宏,您可以选择使用&#34;跟踪更改&#34;功能而不是写宏。 (注意:无法在共享工作簿中编辑宏)。打开此功能后,您可以通过导航到“跟踪更改”&gt;突出显示更改&gt;选择&#34;在新工作表上列出更改&#34;来查看更改。您还可以选择显示所有更改,或仅显示自上次保存工作簿以来的更改。

以下是一个链接,其中包含有关共享工作簿MS Shared Workbooks

支持和不支持的功能的更多信息

“跟踪更改历史记录”工作表示例: Sample History Page from Tracking Changes