首页 > 解决方案 > 有没有办法使用宏跟踪共享工作簿中发生的更改?

问题描述

我有一个可供多个用户访问的共享主表。我想使用宏跟踪用户所做的更改,然后保留第一个用户所做的更改并给其他用户一个 msgbox,说明已经进行了更改。现在有功能区中的“共享工作簿”选项中提供了一个选项,其中显示“询问我哪些更改获胜”,但我希望第一个用户在不弹出任何对话框的情况下进行更改。我尝试了“正在保存的更改”选项,但它没有按预期工作。有没有办法使用宏来做到这一点?如果是,如何?

标签: excelvba

解决方案


在此处输入图像描述

Private Sub Worksheet_Change(ByVal Target As Range)

Set FindCell = Worksheets("Track Changes").Columns(2).Find(Target.Address, LookIn:=xlValues)
R = Worksheets("Track Changes").Cells(Rows.Count, 1).End(xlUp).Offset(1).Row

If FindCell Is Nothing Then
    With Worksheets("Track Changes")
        .Cells(R, 1).Value = Date
        .Cells(R, 2).Value = Target.Address
        .Cells(R, 3).Value = Application.UserName
        If Target.Value = "" Then
             .Cells(R, 4).Value = "Empty cell"
        Else
             .Cells(R, 4).Value = Target.Value
        End If
    End With
Else
    firstAddress = FindCell.Address
    Do
        If FindCell.Offset(0, -1).Value = Date Then
            MsgBox "Changes already made by " & FindCell.Offset(0, 1).Value & _
                vbNewLine & "Changes: " & FindCell.Offset(0, 2).Value
            Exit Sub
        End If

        Set FindCell = Worksheets("Track Changes").Columns(2).FindNext(FindCell)
    Loop While Not FindCell Is Nothing And FindCell.Address <> firstAddress

    With Worksheets("Track Changes")
        .Cells(R, 1).Value = Date
        .Cells(R, 2).Value = Target.Address
        .Cells(R, 3).Value = Application.UserName
        If Target.Value = "" Then
             .Cells(R, 4).Value = "Empty cell"
        Else
             .Cells(R, 4).Value = Target.Value
        End If
    End With
End If

End Sub

推荐阅读