Application.Ontime在从子函数调用时起作用,但不从函数调用

时间:2014-02-16 22:39:39

标签: excel vba excel-vba excel-2007 excel-2010

请看一下代码:

 '' the sub I can call manually to see if the whole thing works
 '' (and it does)
Sub ManualSubTest() 
    Call SendNotification 
End Sub 

 '' this is the function I need to refer to from the IF formula
 '' when I do, Ontime does not work
Function SendNotification() 
    SendNotificationAction 
    Debug.Print ("first step...") 
End Function 

 '' the main sub that will perfom the action
Private Sub SendNotificationAction() 
     '' do something here
    Application.OnTime Now + TimeValue("0:00:05"), "SendNotificationCallback" 
End Sub 

 '' this is the callback that normally triggers the action code again
Private Sub SendNotificationCallback() 
     ''SendNotificationAction
     '' or
     ''Call SendNotificationAction
    Debug.Print ("OK!") 
End Sub 

当我从宏菜单中手动调用ManualSubTest子时,一切正常。如您所见,它所做的只是触发SendNotification功能。最后的结果我看到了“OK!”立即窗口中的消息。

但是当我从工作表IF条件(即SendNotification)调用它时,我需要=If($A$1=1; SendNotification(); "")函数才能工作,并且由于某种原因它不会。它只是在Ontime电话之前停止。

有什么区别? Functions vs. Subroutines limitations是否适用?请帮忙。感谢

1 个答案:

答案 0 :(得分:-1)

尝试添加

 application.enableevents=true     

在函数中,就在onaction

之前