工作表审计变更日志

时间:2021-04-30 15:32:43

标签: excel vba

我创建了一个脚本,该脚本为我提供了对我的数据选项卡所做更改的审核日志(我想为用户提供访问权限以进行一次性更改,因此不想完全锁定和保护该选项卡) .

但是,我还有其他几个宏和按钮可以更改数据选项卡,但是,我希望这些更改(由其他宏进行的)不会在我的审核日志中捕获。

本质上,有没有办法让我可以调用 IF 其他宏,不通过自动转到 Exit Sub 来更新审核日志。或者类似的东西。

请参阅下面的审核日志脚本, 其他按钮可以是(将数据波动 x% 或重置按钮)

Dim PreVal

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'This sets our previous value once we have selected the cell value to change
    PreVal = Target.Value
End Sub
    
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim LastRow
    
    'If we change the cell we selected then the worksheet change event is triggered
    If Target.Value <> PreVal Then
    
        LastRow = Worksheets("Logged Changes").Cells(Rows.Count, 2).End(xlUp).Row
        
        'If the new value of the cell is not the same a previous value then logging of details begins
        Worksheets("Logged Changes").Cells(LastRow, 2).Offset(1, 0).Value = _
        Application.UserName & " changed cell " & Target.Address _
        & " from " & PreVal & " to " & Target.Value
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

我创建了一个变通方法 - 可能不是最有效或“最好”的方式 - 但它似乎有效。

Sub Macro1()
Range("Macro").Value = "Running"

    'Rest of Macro1 goes here

Range("Macro").ClearContents

End Sub

'Macro2 (The macro embedded in my sheet) would start with

If Range("Macro").Value = "Running" Then Exit Sub

    'Rest of Macro2


``