Workbook.Save 方法 (Excel)

儲存對指定的活頁簿所做的變更。

語法

運算式

表達 代表 Workbook 物件的 變數。

註解

若要開啟活頁簿檔案,請使用 Open 方法。

若要將活頁簿標示為已儲存而不寫入磁片,請將其 Saved 屬性設定為 True

第一次儲存活頁簿時,請使用 SaveAs 方法來指定檔案的名稱。

範例

本範例會儲存使用中活頁簿。

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 支援與意見反應