Excel VBA通​​知所有只读查看器的更改

时间:2012-01-25 00:23:07

标签: excel vba

我认为如果当一群人以只读方式查看相同的工作簿时,他们可以在每次更新工作簿时通过弹出窗口通知他们,这将是一个很好的方便。这样他们马上知道他们正在看什么可能不再准确。感谢

2 个答案:

答案 0 :(得分:5)

这是一种狡猾的小方法,可以做你想做的事。我们的想法是获取FileDateTime(ThisWorkbook.FullName),即上次修改工作簿文件的日期。您首先在打开工作簿时获取此日期,将其存储在工作簿的单元格中,然后定期检查FileDateTime(ThisWorkbook.FullName)是否返回与存储的日期不同的日期。

在此示例中,我将日期存储在Sheet1.Range("A1")中,但您可以将其存储在隐藏的工作表中或任何位置。

ThisWorkbook模块中,按如下方式定义Workbook_Open事件:

Private Sub Workbook_Open()
    userNotified = False

    'Store date last modified.
    dateLastModifiedWhenOpened = FileDateTime(ThisWorkbook.FullName)

    'How often will we check back?
    runTimeInterval = TimeValue("00:00:05")

    'Set timer for next check.
    Application.OnTime Now + runTimeInterval, _
        "CheckWhetherThisWorkbookFileModifiedSinceOpening"
End Sub

在代码模块中:

Public dateLastModifiedWhenOpened As Date
Public nextRunTime As Date
Public runTimeInterval As Date
Public userNotified As Boolean

Sub CheckWhetherThisWorkbookFileModifiedSinceOpening()

    If Not FileDateTime(ThisWorkbook.FullName) = dateLastModifiedWhenOpened Then
        MsgBox "This workbook file has been modified since you opened it." _
            & vbCrLf & "Modified at: " & FileDateTime(ThisWorkbook.FullName)
        userNotified = True
    Else
        'Set timer for next check.
        nextRunTime = Now + runTimeInterval
        Application.OnTime nextRunTime, _
            "CheckWhetherThisWorkbookFileModifiedSinceOpening"
    End If

End Sub

关闭工作簿时清理可能是个好主意。在ThisWorkbook模块中:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Not userNotified Then
        'Cancel the next check.
        Application.OnTime nextRunTime, _
            "CheckWhetherThisWorkbookFileModifiedSinceOpening", , False
    End If
End Sub

答案 1 :(得分:1)

您可以通过“审核”功能区“共享工作簿”共享工作簿。

在高级选项中,您可以将“更新更改”设置为5分钟。在您的情况下,您可能想要“只看其他用户的更改”。