如何在Word文檔中計算兩個日期或時間之間的天數或小時數?
通常,我們可以快速輕鬆地在Excel工作表中計算兩個日期之間的天數,但是,您是否嘗試過在Word文檔中獲取兩個指定日期之間的天數呢?
使用VBA代碼計算Word文檔中兩個日期之間的差異
要計算兩個指定日期之間的天數,以下VBA代碼可以幫助您,請按照以下步驟操作:
1. 按住 ALT + F11鍵打開Microsoft Visual Basic for Applications窗口。
2. 然後,點擊 插入 > 模塊,將以下代碼複製並粘貼到打開的空白模塊中:
VBA代碼:計算兩個日期之間的差異
Sub CalculateDateDifference()
Dim xStartDate As Date
Dim xEndDate As Date
Dim xDay As Long
On Error Resume Next
xStartDate = InputBox("Enter the start date", "KuTools for Word", "")
xEndDate = InputBox("Enter the end date", "KuTools for Word", "")
If (InStr(1, Str(xStartDate), ":") > 0) Or (InStr(1, Str(xEndDate), ":") > 0) Then
MsgBox "please input current date", vbInformation, "KuTools for Excel"
Exit Sub
End If
xDay = DateDiff("d", xStartDate, xEndDate)
MsgBox "There are " & xDay & " days left from " & xStartDate & " to " & xEndDate & vbCrLf, vbInformation, "KuTools for Word"
End Sub
3. 然後,按F5鍵運行此代碼,在連續出現的對話框中,輸入您想使用的開始日期和結束日期,參見截圖:
4. 然後,點擊 確定按鈕,您將獲得想要的結果,參見截圖:
使用VBA代碼計算Word文檔中兩個時間之間的差異
這裡有另一個VBA代碼,可以幫助您計算兩個指定時間之間的差異,請按照以下步驟操作:
1. 按住ALT + F11鍵打開Microsoft Visual Basic for Applications窗口。
2. 然後,點擊 插入 > 模塊,將以下代碼複製並粘貼到打開的空白模塊中:
VBA代碼:計算兩個時間之間的差異
Sub CalculateTimeDifference()
Dim xStartDate As Date
Dim xEndDate As Date
Dim xTime As Long
Dim xHour As Long
On Error Resume Next
xStartDate = InputBox("Enter the start time", "KuTools for Word", "")
xEndDate = InputBox("Enter the end time", "KuTools for Word", "")
Debug.Print Str(xStartDate)
If (Str(xStartDate) = " 0:00:00") Or (Str(xEndDate) = " 0:00:00") _
Or (Str(xStartDate) = " 12:00:00 AM") Or (Str(xEndDate) = " 12:00:00 AM") Then
MsgBox "please input the time", vbInformation, "KuTools for Excel"
Exit Sub
ElseIf xStartDate > xEndDate Then
MsgBox " The start time is not larger than the end time!", vbInformation, "KuTools for Excel"
Exit Sub
End If
xTime = DateDiff("s", xStartDate, xEndDate)
xHour = xTime \ 3600
xTime = xTime - xHour * 3600
MsgBox "There are " & xHour & " hours " & xTime \ 60 & " minutes " & xTime - (xTime \ 60) * 60 _
& " seconds left from " & xStartDate & " to " & xEndDate & vbCrLf, vbInformation, "KuTools for Word"
End Sub
3. 然後,按F5鍵運行此代碼,在連續出現的對話框中,輸入您想用來計算差異的開始時間和結束時間,參見截圖:
4. 然後,點擊確定按鈕,兩個指定時間之間的時間差已經被計算並顯示如下截圖所示:
最佳辦公效率工具
Kutools for Word -透過超過 100 項卓越功能,提升您的 Word 使用體驗!
🤖 Kutools AI Features:AI Assistant/Real-Time Assistant/Super Polish(保留格式)/Super Translate(保留格式)/AI Redaction/AI Proofread...
📘 文件掌握:分割頁面/文檔合併/區域導出成各種格式(PDF/TXT/DOC/HTML...)/批量轉換為 PDF...
✏ 正文編輯:批量查找與替換多個文件/批量調整所有圖片大小/翻轉表格的行與列/表格轉文本...
🧹 輕鬆清理:快速清除多餘空格/分節符/文字框/超連結/更多清除工具請前往刪除工具組...
➕ 創意插入:插入千位分隔符/複選框/選項按鈕/二維碼/條形碼/批量插入圖片/還有更多精彩功能在插入工具組...
🔍 精確選擇:精確定位特定頁面/表格/形狀/標題段落/使用更多 選擇 功能提升導航效率...
⭐ 亮點增強:導航至任何區域/自動插入重複文字/切換文檔窗口/11 項轉換工具...
🌍 支援40+種語言:可用 Kutools於您偏好語言環境——支援英語、西班牙語、德語、法語、中文以及超過40種語言!

✨ Kutools for Office——一次安裝,五大強力工具!
包含 Office Tab Pro · Kutools for Excel · Kutools for Outlook · Kutools for Word · Kutools for PowerPoint
📦 五大工具一次全擁有 | 🔗 與 Microsoft Office 無縫整合 | ⚡ 即刻省時提高工作效率
最佳辦公效率工具
Kutools for Word - 100+ 款 Word 工具
- 🤖 Kutools AI Features:AI Assistant/Real-Time Assistant/Super Polish/Super Translate/AI Redaction/AI Proofread
- 📘 文件掌握:分割頁面/文檔合併/批量轉換為 PDF
- ✏ 正文編輯:批量查找與替換/批量調整所有圖片大小
- 🧹 輕鬆清理:清除多餘空格/清除分節符
- ➕ 創意插入:插入千位分隔符/插入複選框/創建二維碼
- 🌍 支援40+種語言:Kutools 支援您的語言——可使用超過40種語言!