跳到主要内容

如何计算Word文档中两个日期或时间之间的天数或小时数?

通常,我们可以快速轻松地计算Excel工作表中两个日期之间的天数,但是,您是否曾经尝试过获取Word文档中两个给定日期之间的天数?

使用VBA代码计算Word文档中两个日期之间的差异

使用VBA代码在Word文档中计算两次之间的差异


使用VBA代码计算Word文档中两个日期之间的差异

要计算两个给定日期之间的天数,请按照以下VBA代码进行操作,具体操作如下:

1。 按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。

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 键以运行此代码,在连续的对话框中,输入要使用的开始日期和结束日期,请参见屏幕截图:

doc计算天数1

4。 然后,点击 OK 按钮,您将获得所需的结果,请参见屏幕截图:

doc计算天数2


使用VBA代码在Word文档中计算两次之间的差异

这是另一个VBA代码,可以帮助您计算两次给定时间之间的时差,请按照以下步骤操作:

1。 按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。

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 键以运行此代码,在连续的对话框中,输入要用于计算差异的开始时间和结束时间,请参见屏幕截图:

doc计算天数3

4。 然后,点击 OK 按钮,并且已计算出两个给定时间之间的时差,并显示如下屏幕截图所示:

doc计算天数4

最佳办公生产力工具

Kutools for Word - 通过 Over 提升您的文字体验 100 显着特点!

🤖 Kutools 人工智能助手:用人工智能改变你的写作 - 生成内容  /  重写文本  /  总结文件  /  查询资料 基于文档,全部在Word中

📘 文档掌握: 分页  /  合并文件  /  以各种格式导出选择(PDF/TXT/DOC/HTML...)  /  批量转换为PDF  /  将页面导出为图像  /  一次打印多个文件...

内容编辑: 批量查找和替换 跨多个文件  /  调整所有图片的大小  /  转置表行和列  /  将表格转换为文字...

🧹 轻松清洁: 移开 多余的空间  /  分节符  /  所有标题  /  文本框  /  超链接  / 如需更多拆卸工具,请前往我们的 删除组...

创意插入: 插 千位分隔符  /  复选框  /  单选按钮  /  扫码支付  /  条码  /  对角线表  /  公式标题  /  图片说明  /  表标题  /  多张图片  / 发现更多 插入组...

🔍 精准选择:精确定位 特定页面  /    /  形状  /  标题段落  / 增强导航功能 更多 选择功能...

星级增强: 快速导航至任何位置  /  自动插入重复文本  /  在文档窗口之间无缝切换  /  11 转换工具...

👉 想尝试这些功能吗? Kutools for Word 提供了 60-day免费试用,没有任何限制! 🚀
 
Comments (0)
No ratings yet. Be the first to rate!
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations