Excel 以14,17,44,47分钟的速度每小时运行第一个宏,以22,52分钟的速度运行第二个宏
这里我有我糟糕的代码,问题还在于当它运行和等待时25%的Cpu使用率Excel 以14,17,44,47分钟的速度每小时运行第一个宏,以22,52分钟的速度运行第二个宏,excel,vba,Excel,Vba,这里我有我糟糕的代码,问题还在于当它运行和等待时25%的Cpu使用率 Public StopRunning As Boolean Sub somebutton_click() StopRunning = True End Sub Sub AutoOn() Application.DisplayAlerts = False Application.Wait (Now + TimeValue("00:14:00")) Call Wholeshort
Public StopRunning As Boolean
Sub somebutton_click()
StopRunning = True
End Sub
Sub AutoOn()
Application.DisplayAlerts = False
Application.Wait (Now + TimeValue("00:14:00"))
Call Wholeshort
Application.Wait (Now + TimeValue("00:03:00"))
Call Wholeshort
Application.Wait (Now + TimeValue("00:05:00"))
Call Whole
Application.Wait (Now + TimeValue("00:22:00"))
Call Wholeshort
Application.Wait (Now + TimeValue("00:03:00"))
Call Wholeshort
Application.Wait (Now + TimeValue("00:05:00"))
Call Whole
Application.DisplayAlerts = True
If Not StopRunning Then Application.OnTime Now + TimeValue("00:08:00"), "AutoOn"
End Sub
我希望在xx:14、xx:17、xx:44、xx:47分钟时每小时运行一次Macro1,在xx:22、xx:52分钟时每小时运行一次Macro2,并通过按钮停止该sub。所以当我在14:55启动sub时,它将开始运行15:14,15:17,15:22。。。。内联但可通过按钮停止(如何指定按钮快捷方式停止,如ctrl+x)
是否有一种更有效的方法来编写此代码,并且我可以随时启动此程序,而不是每次都调整等待分钟数取决于何时启动此程序,我需要在编写时每小时运行此程序
谢谢大家! 请尝试这种方法:
工作簿\u Open()
事件或任何其他子事件开始:OnTime
调用,您可以使用以下步骤:
Sub somebutton_click()
StopRunning = True
End Sub
不要使用
应用程序。等待。使用Application.OnTime
在每个连续的sub中调用下一个sub…谢谢,但是有没有更好的方法可以每小时运行XX小时14分钟?是的!我将发布一个答案来更好地解释逻辑…嗨,检测到模棱两可的名称:whisthort。。。因为我的宏名是完整的,所以它们被更改为module2.whole以与此模块4分开。。。。代码不会运行超过2。part@Moment胡德:这样的错误是正常的,如果你有两个同名的过程。。。我认为您将只附加现有的过程。如果您使用了不同的模块,为了避免错误,您必须将讨论中的过程声明为Private
(正如我在修改的代码中所做的那样)。但是你也必须将现有的私有化。即使它们存在于另一个模块中…我为两个宏都放置了私有子模块,在您的代码中,前面也有私有子模块。。。。仍然显示不明确error@Moment胡德:在你抱怨“模棱两可的错误”后,我编辑了代码。。。但是,当出现此类错误时,必须选择同样存在于其他位置的代码部分。它选择什么字符串?如果有一个涉及的子项,请按Ctrl+F
,选择“当前项目”,然后查看哪里有双重(可能是三重)曝光。将所有文件设为私有文件,或更改其名称,在名称结尾处添加内容(“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。。。。为什么你的代码子程序和我的宏同名?我只有VBA项目模块中的2个宏,没有个人工作簿
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:14:00"), "Wholeshort"
End Sub
Private Sub Wholeshort()
'existing procedure code...
'....
wCount = wCount + 1
If Not StopRunning Then
If wCount Mod 2 = 0 Then
'second time (even) it calls Whole:
Application.OnTime Now + TimeValue("00:02:00"), "Whole"
wCount = 0
Else
'first time (odd) it calls itself
Application.OnTime Now + TimeValue("00:03:00"), "Wholeshort"
End If
End If
End Sub
Private Sub Whole()
'existing procedure code...
'....
If Not StopRunning Then Application.OnTime Now + TimeValue("00:22:00"), "Wholeshort"
End Sub
Sub somebutton_click()
StopRunning = True
End Sub