【问题标题】:How to make track-changes column/range specific如何使跟踪更改列/范围特定
【发布时间】:2019-07-07 09:52:48
【问题描述】:

我有一个包含许多列和行的表,我想跟踪在特定列中所做的更改。我在网上找到了代码(见消息底部),它运行良好。但是,它会跟踪“所有更改”。有没有办法使其范围特定。例如,我只想用红色数字跟踪列中所做的更改。请看示例:

我在网上找到的代码如下:

Option Explicit

Dim vOldVal 'Must be at top of module

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim bBold As Boolean
    If Target.Cells.Count > 1 Then Exit Sub
    If ActiveSheet.Name = "Pricing" Then Exit Sub
    'On Error Resume Next
    With Application
         .ScreenUpdating = False
         .EnableEvents = False
    End With
    If IsEmpty(vOldVal) Then vOldVal = "Empty Cell"
    bBold = Target.HasFormula
        With Sheets("Data")
            '.Unprotect Password:="Secret"
                If .Range("A96") = vbNullString Then
                    .Range("A96:H96") = Array("Cell Changed", "Old Value", _
                        "New Value", "Old Formula", "New Formula", "Time of Change")
                End If
            With .Cells(.Rows.Count, 1).End(xlUp)(2, 1)
                  .Value = ActiveSheet.Name & " : " & Target.Address
                  .Offset(0, 1) = vOldVal
            With .Offset(0, 2)
              If bBold = True Then
                .ClearComments
                .AddComment.Text Text:= _
                     "OzGrid.com:" & Chr(10) & "" & Chr(10) & _
                        "Bold values are the results of formulas"
              End If
                .Value = Target
                .Font.Bold = bBold

            End With
                .Offset(0, 3) = Time
                .Offset(0, 4) = Date
                .Offset(0, 5) = Application.UserName
            End With
            .Cells.Columns.AutoFit
            '.Protect Password:="Secret"
        End With
    vOldVal = vbNullString
    With Application
         .ScreenUpdating = True
         .EnableEvents = True
    End With
    On Error GoTo 0
End Sub


Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
        vOldVal = Target
End Sub


Private Sub test()
    Application.EnableEvents = True
End Sub

请记住,我对 VBA 编码非常陌生。

【问题讨论】:

  • 您希望在哪里跟踪更改?您想要跟踪哪些变化?你想要它改变的时间吗?旧值?
  • 我想要更改的新值、旧值、时间、作者和单元格编号。我希望跟踪更改的位置与表格位于同一文档中的 A96:E96。

标签: excel vba copy range trace


【解决方案1】:

您可以通过在现有代码中添加一行来做到这一点:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim bBold As Boolean
    If Target.Cells.Count > 1 Then Exit Sub
    If ActiveSheet.Name = "Pricing" Then Exit Sub
    If Target.Column <> 4 Then Exit Sub

这只会检查工作表的第 4 列是否已更改,并且会忽略其他地方的更改。

【讨论】:

  • 非常感谢。这正是我想要的。
  • 请注意,工作表更改特定于一张工作表,因此您无需在此处测试工作表。 Target 将永远存在于包含此代码的工作表上。这是一个多余的检查
  • 所以,这意味着你可以减少到If Target.Cells.Count &gt; 1 or Target.Column &lt;&gt; 4 Then Exit Sub
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-24
  • 2015-12-06
  • 2019-11-04
  • 1970-01-01
  • 2012-05-20
相关资源
最近更新 更多