vba 使用系统时间每 30 秒保存一次 excel 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15149041/
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
Save excel file every 30 seconds using system time
提问by DavidJB
I would like to save an excel file every 30 seconds based on the current system time. So at HH:MM:30 and HH:NN:00. I'm aware you can call a macro (to save the workbook) at specific times using TimeValue, for example at 16:30:00 (see below). Does anyone know if this, or another function, can be used to save an open workbook when the current system time updates to XX:XX:30 or XX:XX:00 in excel.
我想根据当前系统时间每 30 秒保存一个 excel 文件。所以在 HH:MM:30 和 HH:NN:00。我知道您可以使用 TimeValue 在特定时间调用宏(以保存工作簿),例如在 16:30:00(见下文)。有谁知道当当前系统时间在 excel 中更新为 XX:XX:30 或 XX:XX:00 时,是否可以使用此功能或其他功能来保存打开的工作簿。
Private Sub Workbook_Open()
Application.OnTime TimeValue("16:30:00"), "macro_save"
End Sub
回答by mkingston
In your workbook code:
在您的工作簿代码中:
Private Sub Workbook_Open()
If Second(Now) < 30 Then
Application.OnTime VBA.TimeSerial(Hour(Now), Minute(Now), 30), "macro_save"
Else
Application.OnTime VBA.TimeSerial(Hour(Now), Minute(Now) + 1, 0), "macro_save"
End If
End Sub
In a standard module:
在标准模块中:
Public Sub macro_save()
ThisWorkbook.Save
If Second(Now) < 30 Then
Application.OnTime VBA.TimeSerial(Hour(Now), Minute(Now), 30), "macro_save"
Else
Application.OnTime VBA.TimeSerial(Hour(Now), Minute(Now) + 1, 0), "macro_save"
End If
End Sub
You'll probably need to handle errors in the macro_save
code, otherwise it could get pretty (very) annoying for the user.
您可能需要处理macro_save
代码中的错误,否则对用户来说可能会变得非常(非常)烦人。