使用 MsgBox 时的运行时错误“1004”方法“OnTime”

我我

如果用户想要结束计时器,我有一个代码可以正常工作,直到我添加了 MsgBox 确认。每次用户启动任务时,用户窗体只显示一个从零开始的计时器。

我尝试替换"myTimer", , , False -> "myTimer", , True但计时器只是在后台继续增加。

-----用户表单-----

Private Sub UserForm_Initialize()
    Call myTimer
    StartTime = Timer
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, _CloseMode As Integer)
    If MsgBox("Are you sure to End Time?", vbYesNo) = vbYes Then
        myTimer_Cancel
    Else
        Cancel = True
    End If
End Sub

- - -模块 - - -

Option Explicit
Public StartTime As Single
Public Sub myTimer()
    Dim elapsedtime As Single
    elapsedtime = Timer - StartTime
    UserForm2.Label2.caption = Format(CDate(elapsedtime / 86400), "hh:nn:ss")
    Application.OnTime Now + timeValue("00:00:01"), "myTimer"
End Sub

Public Sub myTimer_Cancel()
    Application.OnTime Now + timeValue("00:00:01"), "myTimer", , False
End Sub
蒂姆·威廉姆斯

当您取消计时器时,您必须使用与设置时相同的时间,因此:

-----用户表单-----

Private Sub UserForm_Initialize()
    Call myTimer
    StartTime = Timer 
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, _CloseMode As Integer)
    If MsgBox("Are you sure to End Time?", vbYesNo) = vbYes Then
        myTimer_Cancel
    Else
        Cancel = True
    End If
End Sub

- - -模块 - - -

Option Explicit

Public StartTime As Single
Public NextRun ' << store next run time

Public Sub myTimer()
    Dim elapsedtime As Single
    elapsedtime = Timer - StartTime
    UserForm2.Label2.caption = Format(CDate(elapsedtime / 86400), "hh:nn:ss")
    NextRun = Now + timeValue("00:00:01")
    Application.OnTime NextRun, "myTimer"
End Sub

Public Sub myTimer_Cancel()
    Application.OnTime NextRun, "myTimer", , False
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

VBA:获取运行时1004:使用单元格时对象“ _Worksheet”的方法“ Range”失败

使用boost :: wave时的运行时错误消息

运行时错误“ 1004”:使用ThisWorkbook的范围类的选择方法失败

使用PyPlot时的Julia运行时错误

将Pastespecial与sourcetheme一起使用时出现运行时错误1004

Workbook Auto Open + MsgBox的运行时错误作为用户的响应

在vba msgbox中不使用goto循环的方法

在Excel数据连接中使用VBA更改命令文本SQL时收到运行时错误(1004)

使用Trim时出现运行时错误13

使用两个索引/匹配时,运行时错误1004无法获取match属性

运行时错误13,类型不匹配MsgBox取消

使用Excel-VBA宏从工作簿复制时出现运行时错误'1004'

使用VBA粘贴时如何修复运行时错误1004

运行时错误1004无法使用url链接名称查找列

在范围对象中使用完全限定的单元格时出现运行时错误'1004'

使用Excel VBA在工作簿中查找匹配项:运行时错误1004

项目消失后运行msgbox

Autohotkey:MsgBox“完成”后运行

应用程序打开时出现Microsoft Excel VBA错误运行时1004,导致无休止的错误循环,导致无法使用Excel

结合使用多个工作簿和选择的运行时错误1004

如何使用msgbox VBA提示

使用'WScript.Shell'对象的'Run'方法时出现运行时错误

在Scala中使用anorm时发生运行时错误

尝试使用pip时出现运行时错误

错误 13 使用 MsgBox 显示范围

使用动态数组时出现运行时错误 1004

使用参数方法时的运行时错误

Excel VBA - 使用 .ModifyAppliesTo 和运行时错误 1004(尝试插入太长的字符串时出现问题?)

Excel 使用 VBA 第二次尝试打印时出现运行时错误“1004”粘贴范围类的特殊方法失败