vba excel vba中的秒表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20880645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:10:33  来源:igfitidea点击:

Stopwatch in excel vba?

excelvbastopwatch

提问by user3152805

I'm trying to set up a basic stopwatch for tracking elapsed time. Button1 should start the clock at 0, Button2 stops the watch, and records how many minutes and seconds have elapsed since the timer started. I have the buttons lodged on a worksheet, and just need a way to calculate elapsed time. What would be the best approach? Here is the code i have been working with

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

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

The problem with this code is that it display the time elapsed only after i click button2. I need a running timer on my excel sheet.

此代码的问题在于它仅在我单击 button2 后才显示经过的时间。我的 excel 表上需要一个正在运行的计时器。

回答by Nagaraj S

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

YouTube

link

关联

回答by Dick Kusleika

If you need any accuracy, Excel is probably not the place to do this. But if close enough is close enough, then here's how I would do it.

如果您需要任何准确性,Excel 可能不是执行此操作的地方。但是,如果足够接近就足够接近,那么这就是我将如何做到的。

In a standard module:

在标准模块中:

Public gbStop As Boolean
Public gdtStart As Date

Sub UpdateTime()

    Sheet1.Range("rngTimer").Value = Now - gdtStart
    DoEvents

    If Not gbStop Then
        Application.OnTime Now + TimeSerial(0, 0, 1), "UpdateTime"
    End If

End Sub

In Sheet1's class module

在 Sheet1 的类模块中

Private Sub CommandButton1_Click()

    gbStop = False
    gdtStart = Now
    Application.OnTime Now + TimeSerial(0, 0, 1), "UpdateTime"

End Sub

Private Sub CommandButton2_Click()

    gbStop = True

End Sub

When you click Button1, gbStop is set to false, the start time is recorded in a public variable, and the UpdateTime procedure is scheduled to run 1 second later.

当您单击 Button1 时,gbStop 设置为 false,开始时间记录在一个公共变量中,UpdateTime 过程计划在 1 秒后运行。

One second later, UpdateTime runs and the value of the cell named rngTimer is changed to show the elapsed time between Now and the start recorded in Button1_Click. If gbStop is still False, UpdateTime schedules itself to run again in another second.

一秒钟后,UpdateTime 运行并且名为 rngTimer 的单元格的值更改为显示从 Now 到 Button1_Click 中记录的开始之间经过的时间。如果 gbStop 仍然为 False,UpdateTime 将自己安排在另一秒后再次运行。

Eventually, you press Button2 which sets the public variable gbStop to True. The next time Updatetime runs, it doesn't schedule itself to run again.

最后,您按下 Button2,它将公共变量 gbStop 设置为 True。下次更新时间运行时,它不会安排自己再次运行。