跳到主要内容

如何在Excel工作表中创建秒表?

如果工作表中有秒表,您将知道完成工作的时间。 如何在带有“开始”,“停止”和“重置”按钮的工作表中创建秒表,如以下屏幕截图所示? 如果单击“开始”按钮,它将开始计时,如果您单击“停止”按钮,它将停止计时,而“重置”按钮可以帮助您重置时间。 本文,我将讨论如何在Excel中创建简单方便的秒表。

doc创建秒表1

使用VBA代码创建具有三个按钮的秒表


使用VBA代码创建具有三个按钮的秒表

要创建带有“开始”,“停止”和“重置”三个按钮的秒表,请一一进行以下步骤。

首先,插入三个命令按钮。

1。 点击 开发商 > 插页 > 命令按钮,请参见屏幕截图:

doc创建秒表2

2。 然后拖动鼠标绘制一个按钮,插入按钮后,可以更改按钮标题,请单击 开发商 > 查看房源 查看房源 对话框中,输入新标题“Start 开始”按钮,位于旁边的文本框中 标题,请参见屏幕截图:

doc创建秒表3 2 doc创建秒表4

3。 重复上述两个步骤,以插入其他两个按钮并将其标题为“ Stop 停止“和”重设”,请参见屏幕截图:

doc创建秒表5

4。 插入按钮后,应通过单击退出设计模式 开发商 > 设计模式.

其次,创建一个VBA代码。

5。 然后,右键单击当前工作表选项卡,然后选择 查看代码,在弹出 Microsoft Visual Basic应用程序 窗口,请复制以下代码并将其粘贴到 表码:

VBA代码:创建秒表:

Public StopIt As Boolean
Public ResetIt As Boolean
Public LastTime
Private Sub CommandButton1_Click()
Dim StartTime, FinishTime, TotalTime, PauseTime
StopIt = False
ResetIt = False
If Range("C2") = 0 Then
  StartTime = Timer
  PauseTime = 0
  LastTime = 0
Else
  StartTime = 0
  PauseTime = Timer
End If
StartIt:
  DoEvents
  If StopIt = True Then
    LastTime = TotalTime
    Exit Sub
  Else
    FinishTime = Timer
    TotalTime = FinishTime - StartTime + LastTime - PauseTime
    TTime = TotalTime * 100
    HM = TTime Mod 100
    TTime = TTime \ 100
    hh = TTime \ 3600
    TTime = TTime Mod 3600
    MM = TTime \ 60
    SS = TTime Mod 60
    Range("C2").Value = Format(hh, "00") & ":" & Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
    If ResetIt = True Then
      Range("C2") = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
      LastTime = 0
      PauseTime = 0
      End
    End If
    GoTo StartIt
  End If
End Sub
Private Sub CommandButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  StopIt = True
End Sub
Private Sub CommandButton3_Click()
  Range("C2").Value = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
  LastTime = 0
  ResetIt = True
End Sub

doc创建秒表6

备注:在上面的代码中, C2 是要插入秒表时间的单元格,并且 命令按钮 1, 命令按钮 2, 命令按钮 3 是按钮名称,您可以从 名称框,请参见屏幕截图:

doc创建秒表7

第三,格式化秒表时间单元。

6。 然后,您应该将时间单元格格式设置为 文本 格式,您可以根据需要更改单元格大小,字体,字体颜色,背景等,请参见屏幕截图:

doc创建秒表8

7。 完成上述步骤后,从现在开始,当您单击 Start 开始 按钮,时间将从现在开始,然后单击 Stop 停止 按钮,它将停止时间, 重设 按钮将重置时间,请参见屏幕截图:

doc创建秒表9


演示:使用VBA代码创建带有三个按钮的秒表

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (32)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
One has to paste the VBA code to the Sheet's VBA space, not to the Module!

=> I suggest correcting, in the article, the "copy and paste the following code into the Module" -> replace Module with Sheet's VBA field, or sth

Otherwise, perfect, thanks!
This comment was minimized by the moderator on the site
Hello, Florian,

Thanks for your comment, we have updated the content for this article.
Thanks again! 😀
This comment was minimized by the moderator on the site
i started. it works. but it doesn't work when i close and open the excel file again. what must i do?
This comment was minimized by the moderator on the site
Hello, mustafa zirek
After inputing the code, you should save the workbook as Excel Macro-Enabled Workbook format, and when you open the workbook next time, please click the Enable Content above the formula bar to activate the code.
Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
Hi! Thank you for this, how do I make it so it populates time in subsequent cells, ex. start/stop time for cell C2, then a different start/stop time for cell c3, c4, c5 etc 
This comment was minimized by the moderator on the site
Bonjour, j'ai voulu intégrer ce chronomètre à une autre macro, déterminer le temps d'utilisation du fichier. La macro se lance mais bloque l'utilisation du fichier.Comment faire pour utiliser votre chronomètre en même temps que d'autres codes VBA?MerciHenry.
This comment was minimized by the moderator on the site
Is it possible to use a combobox or a dropdown selection to start and stop the clock
This comment was minimized by the moderator on the site
This is brilliant! Thanks, Chris H
This comment was minimized by the moderator on the site
Hello
Have followed the steps but cannot get a time to appear in the timer cell. I have copied the code but can i check1. That the line numbers are not needed or are they2. That I put the code into module 1 not sheet1 or This workbook3. Should there be a macro listed in the macro list after doing all this - mine doesn't.
Thanks
This comment was minimized by the moderator on the site
Hi, Janice,You should put the code into your active sheet module as step 5.
5. And then, right click the current worksheet tab, and choose View Code, in the popped out Microsoft Visual Basic for Applications window, please copy and paste the following code into the Module.

And you should change the button name to your own.
Note: In the above code, C2 is the cell where the stopwatch time will be inserted, and the CommandButton1, CommandButton2, CommandButton3 are the button names, you can view the exact button name from the Name Box.

Please check them, thank you!
This comment was minimized by the moderator on the site
Is it possible to get the counter to count only seconds?
So when it reaches 59 seconds, it continues with 60, 61, 62, 63 ....
This comment was minimized by the moderator on the site
is 'timer' a variable here? dont see its relevance in the code.
This comment was minimized by the moderator on the site
How about adding "+10s" & "2x Speed" feature in this code?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations