保存工作簿之前发生此事件。
语法
表达式。BeforeSave (SaveAsUI, 取消)
表达式 表示 工作簿 对象的变量。
参数
| 名称 | 必需/可选 | 数据类型 | 说明 |
|---|---|---|---|
| SaveAsUI | 必需 | Boolean | 如果由于需要保存在工作簿中所做的更改而显示“另存为”对话框,则为 true。 |
| Cancel | 必需 | Boolean | 当事件发生时为 False 。 如果该事件过程将此参数设置为 True ,则该过程完成后将不保存工作簿。 |
返回值
Nothing
示例
此示例在保存工作簿之前询问用户是否保存。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel as Boolean)
a = MsgBox("Do you really want to save the workbook?", vbYesNo)
If a = vbNo Then Cancel = True
End Sub
此示例使用 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 支持和反馈,获取有关如何接收支持和提供反馈的指南。