Vba excel как закрыть файл без сохранения

 

Артем

Гость

#4

27.12.2007 11:47:48

{quote}{login=Лузер}{date=27.12.2007 11:46}{thema=}{post}Артем! Просят закрыть без сохранения, а ты предлагаешь сохранить.  
ActiveWorkbook.Saved = True — говорит, что книга уже сохранена, ничего не сохраняя  
или  
ActiveWindow.Close False — закрыть без сохранения{/post}{/quote}  
ага, ошибся, сорри

Home / VBA / VBA Close Workbook (Excel File)

To close an Excel file, you need to use the “Close” method. With this method you can, specify if you want to save the changes or not. And, if you want to save and close a file that is not saved yet you can specify the path where you want to save it before closing.

The following is the syntax for the close method.

Workbook.Close (SaveChanges, FileName, RouteWorkbook)

Steps to Close a Workbook

  1. Specify the workbook that you want to close.
  2. Use the close method with that workbook.
  3. In the code method, specify if you want to save the file or not.
  4. In the end, mention the location path where you want to save the file before closing.

In this tutorial, we will look at different ways that you can use to close a workbook in Excel using VBA.

Helpful Links: Run a Macro – Macro Recorder – Visual Basic Editor – Personal Macro Workbook

Close a Workbook without Saving

If you want to save the active workbook without saving you need to use code like the following.

ActiveWorkbook.Close SaveChanges:=False

In this code, I have specified the “False” for the “SaveChanges” argument. So VBA will ignore if there are any changes in the workbook which are not saved. And if you want to close a specific workbook you can use the name of that workbook. Just like the following code.

Workbooks("book1").Close SaveChanges:=False

If you have data in the workbook and you skip the “SaveChanges” argument, then Excel will show a dialog box to confirm if you want to save the workbook or not. The point is: It is better to specify the “SaveChanges” argument even if it’s optional.

Close a Workbook after Saving

As you have seen, there’s an argument in the CLOSE method to specify the path location. Let’s say if you wish to save the “Book6” to the folder on the desktop. Here’s the code that you need to use.

Workbooks("Book6").Close _
SaveChanges:=True, _
Filename:="C:UsersDellDesktopmyFoldermyFile.xlsx"

This code is going to save the workbook “Book6” into the folder that is saved on my desktop with the name “myFIle.xlsx”. But here’s one thing that you need to take care of: IF you already have a workbook with the same name then it will replace that file with the new one.

Don’t worry, there’s a solution that you can use. The following code checks if there’s any file exists with the name that you want to use

Sub vba_close_workbook()
Dim wbCheck As String
wbCheck = Dir("C:UsersDellDesktopmyFoldermyFile.xlsx")
If wbCheck = "" Then
    Workbooks("Book6").Close _
    SaveChanges:=True, _
    Filename:="C:UsersDellDesktopmyFoldermyFile.xlsx"
Else
    MsgBox "Error! Name already used."
End If
End Sub

More on VBA Workbooks

VBA Save Workbook | VBA Delete Workbook | VBA ThisWorkbook | VBA Rename Workbook | VBA Activate Workbook | VBA Combine Workbook | VBA Protect Workbook (Unprotect) | VBA Check IF a Workbook is Open | VBA Open Workbook | VBA Check IF an Excel Workbook Exists in a Folder| VBA Create New Workbook (Excel File)

  • VBA Workbook

Whenever the code is executed while multiple workbooks are open, office stops working with a message

Microsoft Office Excel has stopped working

Windows can try to recover your information and restart the program.

What’s wrong with the code? I’m using MS Office 2007 on Windows7

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    close_without_saving
End Sub

Sub close_without_saving()
    Application.DisplayAlerts = False
    ThisWorkbook.Saved = True

    If Application.Workbooks.Count < 2 Then
        Application.Quit
    Else
        ThisWorkbook.Close
    End If
End Sub

Community's user avatar

asked May 27, 2016 at 5:11

ZuriPow's user avatar

17

If you want to close the workbook without incorporating changes. Then you can use code like this in workbook module ~

Sub Auto_Close() 
    ThisWorkbook.Saved = True 
End Sub

You can also use this for closing workbook without saving changes.

Sub CloseBook2() 
    ActiveWorkbook.Close savechanges:=False 
End Sub

This routine can be attached to Close X Button. Workbook never closes partially, it will always close with all sheets contained in this workbook. DisplayAlerts = False and subsequently True can be incorporated in the routine. That should not create a problem like

Sub CloseBook() 
    Application.DisplayAlerts = False 
    ActiveWorkbook.Close 
    Application.DisplayAlerts = True 
End Sub 

answered May 27, 2016 at 5:45

skkakkar's user avatar

skkakkarskkakkar

2,7322 gold badges16 silver badges29 bronze badges

2

How to close an Excel workbook using VBA and macros, including how to save the file before you close it or discard any changes.

Sections:

Selecting Which Workbook to Close

Close Workbook While Saving Changes

Close Workbook Without Saving Changes

Let the User Decide to Save Changes or Not

Notes

Selecting Which Workbook to Close

First, we need to tell the macro to choose the current workbook to close or another workbook to close.

Current Workbook

We use this piece of code to close the current or currently active workbook and close that.

Other Workbook

We use this piece of code to close any specific open workbook.

Workbooks("test.xlsx").Close

Replace test.xlsx with the name of the file that you want to close.

Close Workbook While Saving Changes

To have Excel automatically save any changes for the workbook that you want to close, just put True behind the close workbook code from above like this:

ActiveWorkbook.Close True

or, to close a specific file like this:

Workbooks("test.xlsx").Close True

Close Workbook Without Saving Changes

To have an Excel window close WITHOUT saving any changes, just put False behind the close workbook code from above like this:

ActiveWorkbook.Close False

or, to close a specific file like this:

Workbooks("test.xlsx").Close False

Let the User Decide to Save Changes or Not

You use the basic code from the first section and don’t include a True or False after it and a dialog box will open asking if you want to save the file or not; it looks like this:

Close the currently active or visible workbook:

Close a specific workbook:

Workbooks("test.xlsx").Close

Notes

You may run into issues with messages popping up depending on your implementation of this code and your setup and it can help to turn off ScreenUpdating for Excel. Make sure to turn it back on when you are finished though.

If Application.DisplayAlerts is set to False before you close the workbook, you won’t see a popup asking if you want to save it or not before closing it. If this is the case, you may lose data if you wanted to save the file before closing it, so test your code on a sample workbook first.

Download the sample files for this tutorial to test everything out.

Similar Content on TeachExcel

Open Excel Workbook Using VBA Macros

Tutorial:
Simple way to open an Excel workbook using VBA and macros.

Syntax

Workbooks.Open («File…

Macro to get Data from Another Workbook in Excel

Tutorial:
Macro to get data from a workbook, closed or open, over a network or locally on your comp…

Get User Submitted Data from a Prompt in Excel using VBA Macros

Tutorial: How to prompt a user for their input in Excel.
There is a simple way to do this using VBA …

Interactive Clickable Buttons and Interface Without Using VBA/Macros in Excel

Tutorial:
How to make your Excel dashboards and worksheets more interactive and easier to view and …

Loop through a Range of Cells in Excel VBA/Macros

Tutorial: How to use VBA/Macros to iterate through each cell in a range, either a row, a column, or …

Kill Command in Excel (Delete Files Using VBA)

Tutorial:
How to safely remove, delete, kill any Excel file, or other file, using VBA Macros in Exc…

Subscribe for Weekly Tutorials

BONUS: subscribe now to download our Top Tutorials Ebook!

Like this post? Please share to your friends:
  • Vba excel как закрыть книгу с сохранением
  • Vba excel как закрыть userform
  • Vba excel как перевести число в текст
  • Vba excel как очистить ячейку в excel
  • Vba excel как очистить строку