excel - 如何用VBA实现秒表?

标签 excel vba stopwatch

我正在尝试设置一个基本秒表来跟踪耗时。 Button1 应在 0 处启动时钟,Button2 应停止 watch 并记录自计时器启动以来经过了多少分钟和秒。我将按钮放在工作表上,只需要一种方法来计算耗时。最好的方法是什么? 这是我一直在使用的代码:

Private Sub CommandButton1_Click()
    startTime = Timer
End Sub

Private Sub CommandButton2_Click()
    Dim finalTime As Single
    finalTime = Timer - startTime
    MsgBox Format(finalTime, "0.00")
End Sub

此代码的问题在于,它仅在我单击 Button2 后才显示耗时。我需要在 Excel 工作表上运行计时器。

最佳答案

Public Started As Boolean
Dim myTime As Date
Sub StartTimer()
    If Not Started Then
        myTime = Time
        Started = True
    Else
        If MsgBox("Do you really want to restart?", vbYesNo) = vbYes Then
            myTime = Time
        End If
    End If
End Sub
Sub StopTimer()
    If Not Started Then
        MsgBox "The timer is stopped."
    Else
        MsgBox Format(Time - myTime, "hh:mm:ss")
        Started = False
    End If
End Sub

youtube

link

关于excel - 如何用VBA实现秒表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20880645/

相关文章:

c# - 为函数制作时间测量包装类的最佳方法是什么?

excel - 使用索引、匹配和连接创建列表

ms-access - 这如何返回一个空值?

vba - 如何选择刚刚通过 VBA 粘贴到 MS Word 的图像

c++ - 需要帮助使用 system.diagnostics 在 C++ 中创建秒表

php - Ajax中需要多个独立的定时器

sql-server - 请求的操作需要 OLE DB Session 对象... - 通过 ADO 将 Excel 连接到 SQL 服务器

excel - 在 Excel 中创建序列列

excel - 无法理解为什么 VBA 在验证 If 语句时退出我的 For-Next 循环

excel - 在不同工作表中查找一列的最大值并在结果表中报告