Workbook.Save 方法 (Excel)

保存对指定工作簿所做的更改。

语法

expression.Save

表达式 表示 工作簿 对象的变量。

备注

若要打开工作簿文件,请使用 Open 方法。

要将工作簿标记为已保存而不将其写入磁盘,请将其 “已保存” 属性设置为 True。

首次保存工作簿时,请使用 “另存为 ”方法指定文件名。

示例

本示例保存活动工作簿。

ActiveWorkbook.Save

本示例保存所有打开的工作簿,然后关闭 Microsoft Excel。

For Each w In Application.Workbooks 
    w.Save 
Next w 
Application.Quit

此示例使用 BeforeSave 事件在保存工作簿之前验证某些单元格是否包含数据。 若以下每个单元格中均包含数据,则无法保存工作簿:D5、D7、D9、D11、D13 和 D15。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
   'If the six specified cells don't contain data, then display a message box with an error
   'and cancel the attempt to save.
   If WorksheetFunction.CountA(Worksheets("Sheet1").Range("D5,D7,D9,D11,D13,D15")) < 6 Then
      MsgBox "Workbook will not be saved unless" & vbCrLf & _
      "All required fields have been filled in!"
      Cancel = True
   End If
End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。