Skip to main content

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

Author: Xiaoyang Last Modified: 2025-05-12

如果您需要在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功能!立即下載!

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!