使用 VBA 关闭 Excel 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3628252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:45:23  来源:igfitidea点击:

Closing Excel Application using VBA

excelvbaexcel-2007

提问by Brani

I have used the following without success. The active workbook closes, indeed, but the excel window remains open.

我使用了以下方法但没有成功。活动工作簿确实关闭了,但 excel 窗口仍然打开。

Application.ActiveWindow.Close SaveChanges:=False
ActiveWorkbook.Close SaveChanges:=False

Which is the command that terminates the application?

哪个是终止应用程序的命令?

EDIT

编辑

To say a little more: In the workbook Open event I run a macro. I want to terminate the application when that macro finishes. I also tried this without success.

多说一点:在工作簿打开事件中,我运行了一个宏。我想在该宏完成后终止应用程序。我也试过这个没有成功。

Private Sub Workbook_Open()
   Macro_MyJob
   Application.Quit
End Sub

Where should I put this Application.Quit command?

我应该把这个 Application.Quit 命令放在哪里?

回答by variant

I think your problem is that it's closing the document that calls the macro before sending the command to quit the application.

我认为您的问题是它在发送退出应用程序的命令之前关闭了调用宏的文档。

Your solution in that case is to not send a command to close the workbook. Instead, you could set the "Saved" state of the workbook to true, which would circumvent any messages about closing an unsaved book. Note: this does not save the workbook; it just makes it look like it's saved.

在这种情况下,您的解决方案是不发送关闭工作簿的命令。相反,您可以将工作簿的“已保存”状态设置为 true,这将绕过有关关闭未保存书籍的任何消息。注意:这不会保存工作簿;它只是让它看起来像是被保存了。

ThisWorkbook.Saved = True

and then, right after

然后,紧接着

Application.Quit

回答by Leng Keong

To avoid the Save prompt message, you have to insert those lines

为避免出现保存提示消息,您必须插入这些行

Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True

After saving your work, you need to use this line to quit the Excel application

保存您的工作后,您需要使用此行退出 Excel 应用程序

Application.Quit

Don't just simply put those line in Private Sub Workbook_Open() unless you got do a correct condition checking, else you may spoil your excel file.

不要只是简单地将这些行放在 Private Sub Workbook_Open() 中,除非您进行了正确的条件检查,否则可能会损坏您的 excel 文件。

For safety purpose, please create a module to run it. The following are the codes that i put:

为了安全起见,请创建一个模块来运行它。以下是我放的代码:

Sub testSave()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
End Sub

Hope it help you solve the problem.

希望它可以帮助您解决问题。

回答by Michael

Application.Quit 

Should do the trick.

应该做的伎俩。

回答by kc kalama

Sub TestSave()
Application.Quit
ThisWorkBook.Close SaveChanges = False
End Sub

This seems to work for me, Even though looks like am quitting app before saving, but it saves...

这似乎对我有用,即使看起来像在保存之前退出应用程序,但它可以节省...

回答by Dan

I tried a certain sequence that seems to work as you can see below:

我尝试了一些似乎有效的序列,如下所示:

ThisWorkbook.Saved = True
Application.Quit
Application.ActiveWindow.Close SaveChanges:=False
ActiveWorkbook.Close SaveChanges:=False

回答by Panagiotis Chatzikonstantis

In my case, I needed to close just one excel window and not the entire application, so, I needed to tell which exact window to close, without saving it.

就我而言,我只需要关闭一个 excel 窗口而不是整个应用程序,因此,我需要确定要关闭的确切窗口,而不需要保存它。

The following lines work just fine:

以下几行工作得很好:

Sub test_t()
  Windows("yourfilename.xlsx").Activate
  ActiveWorkbook.Close SaveChanges:=False
End Sub

回答by K10

You can try out

你可以试试

ThisWorkbook.Save
ThisWorkbook.Saved = True
Application.Quit

回答by Menon

Sub button2_click()
'
' Button2_Click Macro
'
' Keyboard Shortcut: Ctrl+Shift+Q
'
    ActiveSheet.Shapes("Button 2").Select
    Selection.Characters.Text = "Logout"
    ActiveSheet.Shapes("Button 2").Select
    Selection.OnAction = "Button2_Click"
    ActiveWorkbook.Saved = True
    ActiveWorkbook.Save
    Application.Quit
End Sub