我有一个片段(信用: DanteAmor),用于倒计时和关闭形式。我把userform1改名为Question1。见代码:
UserForm代码:
Private Sub UserForm_Initialize()
Label1.Caption = "00:00:30" 'Write the starting number
Call CountDown
End Sub
Private Sub UserForm_Terminate()
Call CountDown_End
End Sub
模块代码
Sub CountDown()
If Question1.Label1.Caption = "00:00:00" Then
Unload Question1
Exit Sub
End If
Question1.Label1.Caption = Format(TimeValue(Question1.Label1.Caption) - TimeValue("00:00:01"), "hh:mm:ss")
DoEvents
Application.OnTime (Now + TimeValue("00:00:01")), "CountDown"
End Sub
Sub CountDown_End()
On Error Resume Next
Application.OnTime (Now + TimeValue("00:00:01")), "CountDown", , False
End Sub
我的挑战
我将提出问题1至50。我刚刚创建了Question2,但是上面的模块仅适用于Question1。我已经尽我所能让模块与Question2和随后的问题一起工作,但似乎无法修复它。,请有人帮我重写上面的模块,这样它就能处理所有其他问题了吗?
谢谢。
见Question1表单
发布于 2020-05-13 17:17:07
CountDown逻辑是为Question1硬编码的。因此,挑战在于如何弥补这一缺陷。我的第一个想法是将问题作为参数传递,不幸的是,这在使用OnTime时不起作用。但是从本质上讲,通过在模块中定义一个UserForm变量可以实现相同的目标:
UserForm
Option Explicit
Private Sub UserForm_Initialize()
Label1.Caption = "00:00:30"
Set Question = Me
Call CountDown
End Sub
Private Sub UserForm_Terminate()
Call CountDown_End
End Sub模块
Option Explicit
Public Question As UserForm
Public Sub CountDown()
If Question.Label1.Caption = "00:00:00" Then
Unload Question
Exit Sub
End If
Question.Label1.Caption = Format(TimeValue(Question.Label1.Caption) - TimeValue("00:00:01"), "hh:mm:ss")
DoEvents
Application.OnTime (Now + TimeValue("00:00:01")), "CountDown"
End Sub
Public Sub CountDown_End()
On Error Resume Next
Application.OnTime (Now + TimeValue("00:00:01")), "CountDown", , False
End Subhttps://stackoverflow.com/questions/61779406
复制相似问题