首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Excel VBA中用户窗体上的计时器

Excel VBA中用户窗体上的计时器
EN

Stack Overflow用户
提问于 2009-10-13 21:11:09
回答 3查看 41.3K关注 0票数 3

我有一些旧的Excel VBA代码,我想在其中定期运行任务。如果我使用的是VB6,我会使用timer控件。

我找到了Application.OnTime()方法,它对于在Excel工作表中运行的代码工作得很好,但我不能让它在用户表单中工作。该方法永远不会被调用。

如何让Application.OnTime()调用用户窗体中的方法,或者是否有其他方法可以安排代码在VBA中运行?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-10-13 21:13:07

我找到了解决此问题的方法。如果您在一个模块中编写一个方法,该模块只调用用户表单中的一个方法,那么您可以使用Application.OnTime()来调度该模块方法。

有点杂乱无章,但除非有人有更好的建议,否则就行了。

下面是一个例子:

代码语言:javascript
复制
''//Here's the code that goes in the user form
Dim nextTriggerTime As Date

Private Sub UserForm_Initialize()
    ScheduleNextTrigger
End Sub

Private Sub UserForm_Terminate()
    Application.OnTime nextTriggerTime, "modUserformTimer.OnTimer", Schedule:=False
End Sub

Private Sub ScheduleNextTrigger()
    nextTriggerTime = Now + TimeValue("00:00:01")
    Application.OnTime nextTriggerTime, "modUserformTimer.OnTimer"
End Sub

Public Sub OnTimer()
    ''//... Trigger whatever task you want here

    ''//Then schedule it to run again
    ScheduleNextTrigger
End Sub

''// Now the code in the modUserformTimer module
Public Sub OnTimer()
    MyUserForm.OnTimer
End Sub
票数 8
EN

Stack Overflow用户

发布于 2017-07-10 11:10:06

把所有的代码移到一个“定时器”模块怎么样?

代码语言:javascript
复制
Dim nextTriggerTime As Date
Dim timerActive As Boolean

Public Sub StartTimer()
    If timerActive = False Then
        timerActive = True
        Call ScheduleNextTrigger
    End If
End Sub

Public Sub StopTimer()
     If timerActive = True Then
        timerActive = False
        Application.OnTime nextTriggerTime, "Timer.OnTimer", Schedule:=False
    End If
End Sub

Private Sub ScheduleNextTrigger()
    If timerActive = True Then
        nextTriggerTime = Now + TimeValue("00:00:01")
        Application.OnTime nextTriggerTime, "Timer.OnTimer"
    End If
End Sub

Public Sub OnTimer()
    Call MainForm.OnTimer
    Call ScheduleNextTrigger
End Sub

现在您可以从主窗体调用:

代码语言:javascript
复制
call Timer.StartTimer
call Timer.StopTimer

要防止出现错误,请添加:

代码语言:javascript
复制
Private Sub UserForm_Terminate()
    Call Timer.StopTimer
End Sub

这将触发:

代码语言:javascript
复制
Public Sub OnTimer()
    Debug.Print "Tick"
End Sub
票数 0
EN

Stack Overflow用户

发布于 2019-10-09 06:12:02

感谢user1575005 !!

使用Module中的代码设置了一个Timer()进程:

代码语言:javascript
复制
Dim nextTriggerTime As Date
Dim timerActive As Boolean

Public Sub StartTimer()
Debug.Print Time() & ": Start"
If timerActive = False Then
    timerActive = True
    Call ScheduleNextTrigger
End If
End Sub

Public Sub StopTimer()
 If timerActive = True Then
    timerActive = False
    Application.OnTime nextTriggerTime, "OnTimer", Schedule:=False
End If
Debug.Print Time() & ": End"
End Sub

Private Sub ScheduleNextTrigger()
If timerActive = True Then
    nextTriggerTime = Now + TimeValue("00:00:10")
    Application.OnTime nextTriggerTime, "OnTimer"
End If
End Sub

Public Sub OnTimer()
Call bus_OnTimer
Call ScheduleNextTrigger
End Sub


Public Sub bus_OnTimer()
Debug.Print Time() & ": Tick"
Call doWhateverUwant
End Sub

Private Sub doWhateverUwant()

End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1562913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档