跳至主要内容

Kutools for Office — 一套工具,五种功能。事半功倍。

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

Author Xiaoyang Last modified

如果你需要在Excel工作表中使用秒表,它可以帮助你跟踪完成任务所需的时间。想象一下,有一个“开始”、“停止”和“重置”按钮来控制秒表,如下图所示。点击“开始”按钮启动计时器,“停止”按钮暂停计时器,“重置”按钮清除时间。在本文中,我们将逐步向你展示如何使用VBA代码在Excel中创建一个简单但功能齐全的秒表。

A screenshot of an Excel worksheet with a stopwatch containing Start, Stop, and Reset buttons

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


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

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

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

1. 点击“开发工具”>“插入”>“命令按钮”,见截图:

A screenshot showing the Developer tab in Excel with the Insert Command Button option highlighted

2. 然后拖动鼠标绘制一个按钮,插入按钮后,你可以更改按钮的标题,请点击“开发工具”>“属性”,在“属性”对话框中,在“Caption”旁边的文本框中输入新标题“开始”,见截图:

A screenshot showing the Developer tab in Excel with the Properties option highlighted Arrow A screenshot of the Command Button properties dialog in Excel, showing where to set the Caption property to Start

3. 重复上述两个步骤,插入另外两个按钮,并将它们分别命名为“停止”和“重置”,见截图:

A screenshot of three Command Buttons being renamed to 'Start', 'Stop', and 'Reset'

4. 插入按钮后,通过点击“开发工具”>“设计模式”退出设计模式。

其次,创建VBA代码。

5. 接着,右键单击当前工作表标签,选择“查看代码”,在弹出的“Microsoft Visual Basic for Applications”窗口中,请将以下代码复制并粘贴到“Sheet Code”中:

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

A screenshot of the VBA editor with code for a stopwatch in Excel

注意:在上面的代码中,“C2”是秒表时间将插入的单元格,而“CommandButton1”、“CommandButton2”、“CommandButton3”是按钮名称,你可以从“名称框”中查看确切的按钮名称,见截图:

A screenshot showing the Name Box in Excel used to verify the names of buttons added to the worksheet

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

6. 然后你应该将时间单元格格式化为“文本”格式,你可以根据需要更改单元格大小、字体、字体颜色、背景等,见截图:

A screenshot showing how to format a cell for stopwatch time in Excel, including font and background customization

7. 完成上述步骤后,从现在开始,当你点击“开始”按钮时,计时器将启动,点击“停止”按钮时,计时器将停止。点击“重置”按钮将重置时间,见截图:

A screenshot of a stopwatch running in an Excel worksheet with Start, Stop, and Reset buttons in action


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

 
Kutools for Excel:超过 300 种实用工具触手可及!永久免费享受 AI 功能!立即下载!

最佳Office办公效率工具

🤖 Kutools AI 助手:以智能执行为基础,彻底革新数据分析 |代码生成 |自定义公式创建|数据分析与图表生成 |调用Kutools函数……
热门功能:查找、选中项的背景色或标记重复项 | 删除空行 | 合并列或单元格且不丢失数据 | 四舍五入……
高级LOOKUP多条件VLookup|多值VLookup|多表查找|模糊查找……
高级下拉列表快速创建下拉列表 |依赖下拉列表 | 多选下拉列表……
列管理器添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域与列……
特色功能网格聚焦 |设计视图 | 增强编辑栏 | 工作簿及工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按名单发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/倾斜/删除线等)……
15大工具集12项 文本工具添加文本删除特定字符等)|50+种 图表 类型甘特图等)|40+实用 公式基于生日计算年龄等)|19项 插入工具插入二维码从路径插入图片等)|12项 转换工具小写金额转大写汇率转换等)|7项 合并与分割工具高级合并行分割单元格等)| ……
Kutools支持多种语言——可选择英语、西班牙语、德语、法语、中文等40多种语言!

通过Kutools for Excel提升您的Excel技能,体验前所未有的高效办公。 Kutools for Excel提供300多项高级功能,助您提升效率并节省时间。 点击此处获取您最需要的功能……


Office Tab为Office带来多标签界面,让您的工作更加轻松

  • 支持在Word、Excel、PowerPoint中进行多标签编辑与阅读
  • 在同一个窗口的新标签页中打开和创建多个文档,而不是分多个窗口。
  • 可提升50%的工作效率,每天为您减少数百次鼠标点击!

所有Kutools加载项,一键安装

Kutools for Office套件包含Excel、Word、Outlook和PowerPoint的插件,以及Office Tab Pro,非常适合跨Office应用团队使用。

Excel Word Outlook Tabs PowerPoint
  • 全能套装——Excel、Word、Outlook和PowerPoint插件+Office Tab Pro
  • 单一安装包、单一授权——数分钟即可完成设置(支持MSI)
  • 协同更高效——提升Office应用间的整体工作效率
  • 30天全功能试用——无需注册,无需信用卡
  • 超高性价比——比单独购买更实惠