Excel 配置模块
我有一个片段(来源:DanteAmor)用于倒计时和结束表单。我已将userform1重命名为问题1。见代码: 用户表单代码:Excel 配置模块,excel,vba,module,Excel,Vba,Module,我有一个片段(来源:DanteAmor)用于倒计时和结束表单。我已将userform1重命名为问题1。见代码: 用户表单代码: 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 模块
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
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 Sub
我的挑战
我将提出问题1至50。我刚刚创建了问题2,但上面的模块仅适用于问题1。我已经尽我所能让模块处理问题2和后续问题,但似乎无法解决它。请任何人帮助重新编写上述模块,使其能够处理所有其他问题
多谢各位
见问题1表格
问题1的倒计时逻辑是硬编码的。因此,挑战在于如何弥补这一缺陷。我的第一个想法是将问题作为参数传递,不幸的是,当使用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
模块
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
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 Sub
布莱恩,好极了!代码工作得很好,完全符合我的需要。非常感谢你,你真的帮我卸下了一个重担!如果答案已经解决了你的问题,请点击点击向上箭头点击复选标记并进行投票。这向更广泛的社区表明,您已经找到了解决方案。没有义务这么做。复选标记起作用,但向上投票箭头不起作用。上面说,“声誉低于15的人所投的票会被记录下来,但不会改变公开显示的帖子分数”。