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

Wes*_*Wes 1 excel vba

我认为如果一群人以只读方式查看相同的工作簿,那么每次更新工作簿时都会在屏幕上弹出一个通知,这样会很方便.这样他们马上知道他们正在看什么可能不再准确.谢谢

Jea*_*ett 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
Run Code Online (Sandbox Code Playgroud)

在代码模块中:

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
Run Code Online (Sandbox Code Playgroud)

关闭工作簿时清理可能是个好主意.在您的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
Run Code Online (Sandbox Code Playgroud)