使用系统时间每30秒保存一次excel文件 [英] Save excel file every 30 seconds using system time

查看:99
本文介绍了使用系统时间每30秒保存一次excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据当前系统时间每30秒保存一个excel文件。所以在HH:MM:30和HH:NN:00。我知道您可以在特定时间使用TimeValue调用宏(以保存工作簿),例如在16:30:00(请参见下文)。有谁知道当前系统时间在excel中更新为XX:XX:30或XX:XX:00时,是否可以使用此函数或其他函数来保存打开的工作簿。

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.

Private Sub Workbook_Open()

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

End Sub


推荐答案

在您的工作簿代码中:

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

在标准模块中:

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

您可能需要处理 macro_save 代码中的错误,否则可能会使用户非常(非常)烦恼。

You'll probably need to handle errors in the macro_save code, otherwise it could get pretty (very) annoying for the user.

这篇关于使用系统时间每30秒保存一次excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆