Excel VBA轮询/阻止代码在指定时间内运行多次

时间:2015-02-25 20:34:06

标签: excel vba excel-vba

我有另一个在后台运行的代码,当满足某些条件时,它会调用子警报。虽然有多种情况可以调用此代码,但现在发生的事情是警报开始运行的频率是我想要的3秒(由AlertTIMER指示)。这是我解决问题的尝试

Sub Alert()
If Sheets("Sheet1").CommandButton21.Enabled = False And Sheets("Sheet1").CommandButton22.Enabled = False And Sheets("Sheet1").CommandButton25.Enabled = False Then
Beep
Application.Speech.Speak ("Part is done")
Beep
Call AlertTIMER
Exit Sub

ElseIf Sheets("Sheet1").CommandButton21.Enabled = False And Sheets("Sheet1").CommandButton22.Enabled = False Then
Beep
Application.Speech.Speak ("Part is done")
Beep
Call AlertTIMER
Exit Sub

ElseIf Sheets("Sheet1").CommandButton21.Enabled = False And Sheets("Sheet1").CommandButton25.Enabled = False Then
Beep
Application.Speech.Speak ("Part is done")
Beep
Call AlertTIMER
Exit Sub

ElseIf Sheets("Sheet1").CommandButton22.Enabled = False And Sheets("Sheet1").CommandButton25.Enabled = False Then
Beep
Application.Speech.Speak ("Part is done")
Beep
Call AlertTIMER
Exit Sub

ElseIf Sheets("Sheet1").CommandButton21.Enabled = False Then
Beep
Application.Speech.Speak ("Part is done")
Beep
Call AlertTIMER
Exit Sub

ElseIf Sheets("Sheet1").CommandButton22.Enabled = False Then
Beep
Application.Speech.Speak ("Part is done")
Beep
Call AlertTIMER
Exit Sub

ElseIf Sheets("Sheet1").CommandButton25.Enabled = False Then
Beep
Application.Speech.Speak ("Part is done")
Beep
Call AlertTIMER
Exit Sub
Else
Exit Sub
End If

End Sub

Sub AlertTIMER()
Dim ACountDown As Date
ACountDown = Now + TimeValue("00:00:03")
Application.OnTime ACountDown, "Alert"
End Sub
显然它不起作用哈哈。最初我有一个if语句或者而不是ands ...但是那也没有用。有什么帮助吗?

修改

我相信正在发生的事情是在AlertTIMER子中它计算值ACountDown。在示例中,假设它首先在凌晨12:00:00触发(ACountDown将= 12:00:03 AM),然后将其作为该值(12:00:03 AM)将其下载到Application.OnTime。当另一个事件被触发时,让我们说在上午12:00:01它将ACountDown计算为12:00:04 AM,然后将其作为该值(12:00:04 AM)提交给Application.OnTime。所以最后我有"警报"在12:00:03和12:00:04被触发。解决这个问题的一种方法是,如果代码将ACountDown保存为变量,直到" Alert"是从Application.OnTime触发的。因此,对于上一个示例,它首先计算ACountDown的12:00:03 AM,然后在第二个触发器进入时重置为12:00:04 AM。在12:00:04 AM然后触发" Alert& #34;通过Application.OnTime - 只运行一次。

EDIT2:

解决此问题的另一种方法是,如果有办法实现这一点,将检查是否有Application.OnTime正在运行/活动,如果是,则取消它。代码如下所示:

Sub AlertTIMER()
Dim ACountDown As Date

If Application.OnTime(, "Alert") Is Nothing Then
Else
 Application.OnTime ACountDown, "Alert", , False
End If

ACountDown = Now + TimeValue("00:00:03")
Application.OnTime ACountDown, "Alert"
End Sub

但是If Application.OnTime(, "Alert") Is Nothing Then会返回错误...因为您不能检查它是否为空,因为它不是对象。关于我怎么可能让这个工作的想法?

2 个答案:

答案 0 :(得分:0)

试试这样:

Sub Alert()

    If Sheets("Sheet1").CommandButton21.Enabled = False And Sheets("Sheet1").CommandButton22.Enabled = False And Sheets("Sheet1").CommandButton25.Enabled = False Then
        Beep
        Application.Speech.Speak ("Part is done")
        Beep

    ElseIf Sheets("Sheet1").CommandButton21.Enabled = False And Sheets("Sheet1").CommandButton22.Enabled = False Then
        Beep
        Application.Speech.Speak ("Part is done")
        Beep

    ElseIf Sheets("Sheet1").CommandButton21.Enabled = False And Sheets("Sheet1").CommandButton25.Enabled = False Then
        Beep
        Application.Speech.Speak ("Part is done")
        Beep

    ElseIf Sheets("Sheet1").CommandButton22.Enabled = False And Sheets("Sheet1").CommandButton25.Enabled = False Then
        Beep
        Application.Speech.Speak ("Part is done")
        Beep

    ElseIf Sheets("Sheet1").CommandButton21.Enabled = False Then
        Beep
        Application.Speech.Speak ("Part is done")
        Beep

    ElseIf Sheets("Sheet1").CommandButton22.Enabled = False Then
        Beep
        Application.Speech.Speak ("Part is done")
        Beep

    ElseIf Sheets("Sheet1").CommandButton25.Enabled = False Then
        Beep
        Application.Speech.Speak ("Part is done")
        Beep
    Else
        Exit Sub
    End If

    Call AlertTIMER

End Sub

答案 1 :(得分:0)

Sub AlertTIMER()

Dim StartTick As Single
Dim CurrTick As Single
Dim EndTick As Single

StartTick = Timer
EndTick = StartTick + ("3.00")
Do
    CurrTick = Timer
    DoEvents
Loop Until CurrTick >= EndTick
Call Alert
End Sub

这不是我想要的,但是我认为它完成了工作......这使得“Beep,Part is Done,Beep”能够同步而不是竞争。如果AlertTimer在之前已经被触发时被触发(等待CurrTick到达EndTick),那么EndTick会被重置,所以现在它只被触发一次。