提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何在一定的空闲时间后自动保存并关闭Excel文件?

在某些情况下,例如将Excel文件保留在共享磁盘上,文件仍然由用户打开时,其他人无法编辑工作簿。 那么如何在一定的空闲时间之后自动关闭文件以减轻其他人的工作? 本文将帮助您使用VBA方法。

使用VBA代码在一定空闲时间后自动保存并关闭Excel文件

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。

使用VBA代码在一定空闲时间后自动保存并关闭Excel文件


要在一定的空闲时间后自动保存并关闭Excel文件,请按照以下步骤操作。

1。 打开工作簿,使其在一定的空闲时间后自动保存并关闭。 然后按 其他 + F11 钥匙一起打开 Microsoft Visual Basic for Applications 窗口。

2。 在里面 Microsoft Visual Basic for Applications 窗口,请双击 的ThisWorkbook 在右侧窗格中打开“代码”窗口。 然后将VBA代码复制并粘贴到代码窗口中。 看截图:

VBA代码1:在一定的空闲时间后自动保存并关闭Excel文件

Dim xTime As String
Dim xWB As Workbook

Private Sub Workbook_Open()
'Updated by Extendoffice 2019/1/20
    On Error Resume Next
    xTime = Application.InputBox("Please specify the idle time:", "KuTool For Excel", "00:00:20", , , , , 2)
    Set xWB = ActiveWorkbook
    If xTime = "" Then Exit Sub
    Reset
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    On Error Resume Next
    If xTime = "" Then Exit Sub
    Reset
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    On Error Resume Next
    If xTime = "" Then Exit Sub
    Reset
End Sub

Sub Reset()
    Static xCloseTime
    If xCloseTime <> 0 Then
        ActiveWorkbook.Application.OnTime xCloseTime, "SaveWork1", , False
    End If
    xCloseTime = Now + TimeValue(xTime)
    ActiveWorkbook.Application.OnTime xCloseTime, "SaveWork1", , True
End Sub

3。 然后继续点击 插页 > 模块,并将以下代码复制并粘贴到模块窗口中。 看截图:

VBA代码2:在一定的空闲时间后自动保存并关闭Excel文件

Sub SaveWork1()
'Updated by Extendoffice 2019/1/20
    Application.DisplayAlerts = False
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    
    Application.DisplayAlerts = True
End Sub

4。 按 其他 + Q 键同时关闭 Microsoft Visual Basic for Applications 窗口。

5。 点击 文件 > 另存为 > 浏览。 看截图:

6。 在里面 另存为 对话框中,请选择一个文件夹来保存该文件,并在需要时将其命名为文件名框,然后选择 Excel宏启用工作簿 来自 除上文所 类型 下拉列表,最后点击 保存 按钮。 看截图:

从现在开始,每当打开这本工作簿时, Kutools for Excel 对话框会弹出。 请输入您保存的时间并根据关闭工作簿,然后单击 OK 按钮。

工作簿将在指定的空闲时间后自动保存并关闭。 看截图:


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    robert · 5 months ago
    Is there a way to make this work so even if you not on the sheet like click over to another window the timmer will still close that sheet? It seems like i have to stay on the file for it to work
  • To post as a guest, your comment is unpublished.
    Mark · 6 months ago
    I would recommend identifying the workbook with the workbook file name. This would allow the users to have multiple workbooks open and closing out only the specific workbook that needs to have the time lock.

    Suggestion: Set xwb = Workbooks("<file name>")
    instead of: Set xwb = ActiveWorkbook

    Also, replace all references to "ActiveWorkbook" to the Xwb in the module

    Great code otherwise.
    • To post as a guest, your comment is unpublished.
      Sammy · 3 months ago
      Hi Mark,

      Thanks for posting this amendment - found so many codes for closing inactive workbooks, but all of them seemed to affect the one you don't want to close!

      I'm very new to VBA, so I'm not sure how the code would work to replace the wording 'ActiveWorkbook' within the module. I've tried to replace the wording with 'Xwb' but that doesn't change anything and I may be being daft. Would you mind posting a copy of the code please?

      Thanks
    • To post as a guest, your comment is unpublished.
      crystal · 4 months ago
      Hi Mark,
      Thank you for sharing.
  • To post as a guest, your comment is unpublished.
    VR · 7 months ago
    This does not work when focus in on a particular cell in a sheet. Use case is when user is typing in a cell and leave the excel open with the cursor inside the cell.
    • To post as a guest, your comment is unpublished.
      Rehan · 6 months ago
      Were you able to find a fix for this by any chance?
  • To post as a guest, your comment is unpublished.
    henry · 10 months ago
    Hi Thanks loads for this code. Just one issue. It seems to try to close all open workbooks thus stalls if another workbook close is cancelled or not responded to. have you any touch-ups that can fix this?
    • To post as a guest, your comment is unpublished.
      crystal · 7 months ago
      Hi Henry,
      The code has been updated with the problem solving. Please have a try. Sorry for the inconvenience and thank you for your comment.
    • To post as a guest, your comment is unpublished.
      Gaston · 9 months ago
      I have the same issue... I tried to open a new workbook, and entered some data.... when the time came, both workbooks were closed, but the new one wasn´t saved... Is there a way to fix this?
      Besides that, it works great!! Thanks a lot!!
      • To post as a guest, your comment is unpublished.
        crystal · 7 months ago
        Hi Gaston,
        The code has been updated with the problem solving. Please have a try. Sorry for the inconvenience and thank you for your comment.