我在下面的宏中遇到了问题
Sub RefreshAction()
Range("b7").Select
Application.Run "RefreshCurrentSelection"
Application.OnTime (Now() + TimeValue("00:00:05")), "thisworkbook.Action"
End Sub
第一次运行宏时,单元格刷新,但之后立即收到错误消息
消息:无法运行宏“ C \ Desktop \ XYZ.xlsm'!thisworkbook.Action'。该宏可能在此工作簿中不可用,或者所有宏都可能被禁用。
我已经经历过“信任中心->信任中心设置->宏设置->启用所有宏,但是它不起作用。
还单击“对VBA项目对象模型的信任访问”框。
最佳答案
有关更多详细信息,请参见绝对参考:CPearson OnTime
第一个问题,您需要在OnTime
方法中存储要输入的时间才能停止它。 (在这里我声明了一个公共的TimeToRun作为日期)
第二点要连续使用OnTime
方法,您需要在定时过程结束时(此处为RefreshAllStaticData
)重置计时器。
因此,您的整个代码应如下所示:
Public TimeToRun As Date 'so that TimeToRun can be used in both the functions
Sub RefreshAction()
Range("b7").Select
Application.Run "RefreshCurrentSelection"
DoEvents
'Store the next date of execution in TimeToRun
TimeToRun = Now() + TimeValue("00:00:05")
'Launch the next OnTime
Application.OnTime TimeToRun, "RefreshAllStaticData"
End Sub
Sub RefreshAllStaticData()
'--++-- Place your code here, as it is now --++--
'----Call RefreshAction to reset the OnTime method
'---------to another 5 seconds and keep "looping"
RefreshAction
End Sub
Sub Kill_OnTime()
'Launch this to stop the OnTime method
Application.OnTime _
earliesttime:=TimeToRun, _
procedure:="RefreshAllStaticData", _
schedule:=False
End Sub
关于excel - 无法运行宏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33741488/