Excel VBA 中的超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24137955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:24:59  来源:igfitidea点击:

Timeout in Excel VBA

excelvbaexcel-vba

提问by user3725424

I was wondering if it's possible to create a VBA macro in Excel, which will save the file each X minutes. I've already figured how to initialize the macro after excel startup and I found on the google something like this should pause the macro

我想知道是否可以在 Excel 中创建一个 VBA 宏,它将每 X 分钟保存一次文件。我已经想出了如何在 excel 启动后初始化宏,我在谷歌上发现这样的东西应该暂停宏

Application.Wait(Now + TimeValue("0:10:00"))

Application.Wait(Now + TimeValue("0:10:00"))

but this will also block the user input and he cannot make any changes during that time.

但这也会阻止用户输入,在此期间他无法进行任何更改。

This is not a anti-crash protection , the reason why I need this, is that some users are forgetting to save the document regularly...

这不是防崩溃保护,我需要它的原因是有些用户忘记定期保存文档......

Thank you Francis

谢谢弗朗西斯

采纳答案by Tom R

The 2 examples of Simoco will work great, but if you want to prevent having to deal with unnecessary saves (especially if you're working on network files, or large files), you can do a check everytime there is a change in the worksheet.

Simoco 的 2 个例子会很好用,但如果你想避免处理不必要的保存(特别是如果你正在处理网络文件或大文件),你可以在每次工作表发生变化时进行检查.

Just use the Worksheet_Change function to do that, here is a possible pattern:

只需使用 Worksheet_Change 函数即可,这是一种可能的模式:

Private Sub Worksheet_Change(ByVal Target As Range)


If (Now > TimeStamp)
    ThisWorkbook.SaveAs BlaBlaBlaBlaBla
    TimeStamp = Now + TimeValue("0:10:00")
End If

End Sub

TimeStamp needs to be a global variable defined in each workbook.

TimeStamp 需要是每个工作簿中定义的全局变量。

Btw, make sure that saving your file every X minutes doesn't screw with the undo / redo function of excel. I remember I had unwanted behaviors in the past when using an auto-save function.

顺便说一句,请确保每 X 分钟保存一次文件不会与 excel 的撤消/重做功能混淆。我记得我过去在使用自动保存功能时有过不想要的行为。

Other thought: Google document won't require this type of macro as there is no need to save.

其他想法:Google 文档不需要这种类型的宏,因为不需要保存。