【问题标题】:create a countdown timer in excel 2010 using VBA (excel)使用 VBA (excel) 在 excel 2010 中创建倒数计时器
【发布时间】:2016-05-11 20:49:21
【问题描述】:

我正在使用 Excel 2010,并想在 Excel 中创建一个倒数计时器。代码将通过使用带有宏的按钮来启动和停止。按下“开始”按钮时会出现问题。上面的代码使用 Cell "B1" 作为输入点,因为我只是想让它工作,但每次我尝试时,它总是说: “无法运行宏。此工作簿中可能没有该宏,或者所有宏都可能被禁用”。

是的,我在把它放在这里之前启用了所有宏。我希望能够接受用户输入,并使其成为计时器开始的时间,而不仅仅是使用单元格“B1”。

'Macro for Starting the timer (attached to the start button)
Sub startTimer()
Application.OnTime Now + TimeValue("00:00:01"), "nextTick"
End Sub

'Macro for next second
Sub nextTick()
Sheet1.Range("B1").Value = Sheet1.Range("B1").Value - TimeValue("00:00:01")
startTimer
End Sub

'Macro for stopping the timer (attached to the end button)
Sub stopTimer()
Application.OnTime Now - TimeValue("00:00:01"), "nextTick", , False
End Sub

【问题讨论】:

  • 此代码是在 module 代码表还是 sheet 代码表中?您是否通过右键单击工作表的名称选项卡并选择查看代码到达那里?您是否使用 [alt]+F11 打开 VBE 并使用下拉菜单插入 ► 模块?
  • @Jeeped 在主页面中

标签: excel vba


【解决方案1】:

我对您的代码稍作修改,并将其放入标准模块中:

Public Future As Double

Sub StartTimer()
    Future = Now + TimeSerial(0, 0, 1)
    Application.OnTime earliestTime:=Future, procedure:="nextTime", _
         schedule:=True
End Sub

Sub nextTime()
    Sheet1.Range("B1").Value = Sheet1.Range("B1").Value - TimeValue("00:00:01")
    StartTimer
End Sub

Sub StopTimer()
   On Error Resume Next
   Application.OnTime earliestTime:=Future, _
       procedure:="nextTime", schedule:=False
End Sub

StartTimer()StopTimer() 都运行良好。

【讨论】:

  • 感谢您抽出宝贵时间查看此内容,这与宣传的完全一样。
  • 我想知道如何在运行时让它工作,例如,我想获取用户输入并将其作为时间变量输入,然后一旦所有其他模块完成,然后这个模块将被执行。
  • @MTB 我不久前写了一些代码来完全按照您的描述进行操作。代码以 为单位请求时间,然后使用它。
猜你喜欢
  • 2019-08-11
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多