"How To Suppress Save Changes" Prompt When You Close A ...
Có thể bạn quan tâm
Summary
In Microsoft Excel, you can create a Microsoft Visual Basic for Applications (VBA) macro that suppresses the Save Changes prompt when you close a workbook. This can be done either by specifying the state of the workbook Saved property, or by suppressing all alerts for the workbook.
More Information
NOTE: Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure. However, they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To prepare for implementing either of the examples below, perform these steps first:
-
Start Excel and open a new workbook.
-
Press ALT+F11 to start the Visual Basic editor.
-
On the Insert menu, click Module.
-
Type the sample macro code into the module sheet.
-
Press ALT+F11 to return to Excel.
-
In Microsoft Office Excel 2003 and in earlier versions of Excel, choose Macro from the Tools menu, and then click Macros. In Microsoft Office Excel 2007, click Macros in the Code group on the Developer tab. If the Developer tab is not available, consider doing this: a. Click the Microsoft Office Button, and then click Excel Options. b. In the Popular category, under Top options for working with Excel, click to select the Show Developer tab in the Ribbon check box, and then click OK.
-
Select the macro that you want, and then click Run.
The Saved property returns the value False if changes have been made to a workbook since it was last saved. You can use the reserved subroutine name Auto_Close to specify a macro that should run whenever a workbook is closed. In doing so, you can control how the document is handled when the user closes the documents in Excel.
Example 1: Close the workbook without saving changes
To force a workbook to close without saving any changes, type the following code in a Visual Basic module of that workbook:
Sub Auto_Close()
ThisWorkbook.Saved = True
End Sub
When the Saved property is set to True, Excel responds as though the workbook has already been saved and no changes have occurred since that last save. The DisplayAlerts property of the program can be used for the same purpose. For example, the following macro turns DisplayAlerts off, closes the active workbook without saving changes, and then turns DisplayAlerts on again.
Sub CloseBook()
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub
You can also use the SaveChanges argument of the Close method. The following macro closes the workbook without saving changes:
Sub CloseBook2()
ActiveWorkbook.Close savechanges:=False
End Sub
Example 2: Close the workbook and save the changes
To force a workbook to save changes, type the following code in a Visual Basic module of that workbook:
Sub Auto_Close()
If ThisWorkbook.Saved = False Then
ThisWorkbook.Save End If
End Sub
This subprocedure checks to see if the file Saved property has been set to False. If so, the workbook has been changed since the last save, and those changes are saved.
Need more help?
Want more options?
DiscoverCommunityExplore subscription benefits, browse training courses, learn how to secure your device, and more.
Microsoft 365 subscription benefits
Microsoft 365 training
Microsoft security
Accessibility center
Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.
Ask the Microsoft Community
Microsoft Tech Community
Windows Insiders
Microsoft 365 Insiders
Was this information helpful?
Yes No (Send feedback to Microsoft so we can help.) What affected your experience? Resolved my issue Clear instructions Easy to follow No jargon Pictures helped Other Didn't match my screen Incorrect instructions Too technical Not enough information Not enough pictures Other Any additional feedback? (Optional) Submit feedback By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.Thank you for your feedback!
×Từ khóa » đóng File Excel Bằng Vba
-
Đóng Và Mở File Excel Bằng VBA - Freetuts
-
Đóng Và Mở File Excel Bằng VBA - ự Học Excel VBA
-
Đóng Và Mở File Excel Bằng VBA - Freetuts
-
Tạo File Excel đóng Tất Cả Các Workbook đang Mở Bằng VBA
-
Tổng Hợp Các Code VBA Excel Thao Tác Với Workbook Chỉ Trong Nháy ...
-
Đối Tượng Workbook Trong Excel VBA - Học Excel Online Miễn Phí
-
Cách Sao Chép Nội Dung Từ Workbook đang đóng
-
Mở File Excel Trong VBA - VietTuts
-
Các Sự Kiện Và Thao Tác Với Workbook Bằng VBA
-
Đóng Tất Cả Các File Excel đang Mở Mà Không Cần Lưu Lại - Infocom
-
Cách Kết Nối Để Lấy Dữ Liệu Từ File Excel Đang Đóng Bằng Code ...
-
(PDF) Sách Lập Trình Excel Bằng VBA | Vo Duong
-
[Tool Ex Test]Mở Một File Excel Khác Và Lấy Thông Tin ở File đó ...
-
Tự động Lưu Và đóng Tệp Excel Sau Một Thời Gian Nhàn Rỗi Nhất định ...