简体   繁体   English

使用系统时间每30秒保存一次excel文件

[英]Save excel file every 30 seconds using system time

I would like to save an excel file every 30 seconds based on the current system time. 我想根据当前系统时间每30秒保存一个excel文件。 So at HH:MM:30 and HH:NN:00. 所以在HH:MM:30和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). 我知道您可以在特定时间使用TimeValue调用宏(以保存工作簿),例如在16:30:00(请参见下文)。 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. 有谁知道当前系统时间在excel中更新为XX:XX:30或XX:XX:00时,是否可以使用此函数或其他函数来保存打开的工作簿。

Private Sub Workbook_Open()

Application.OnTime TimeValue("16:30:00"), "macro_save"

End Sub

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代码中的错误,否则可能会使用户感到非常(非常)恼人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM