无法使用宏关闭Excel

时间:2018-10-14 00:16:13

标签: excel vba excel-vba

我知道类似的问题已经在这里,但是他们的解决方案对我不起作用

这让我很头疼,并且已经在Google上搜索了很多

我需要按宏关闭Excel

文件已关闭,但程序仍处于打开状态

这是我拥有的代码,我不知道为什么它不能关闭软件,并且没有其他窗口可以打开

Private Sub Workbook_BeforeClose(Cancel As Boolean)
   With ActiveWindow
     .DisplayHeadings = True                            'UnHides Row/Column Headings
     .DisplayHorizontalScrollBar = True                 'UnHides Horizontal Scroll Bar
     .DisplayVerticalScrollBar = True                   'UnHides Vertical Scroll Bar
     .DisplayWorkbookTabs = True                        'UnHides Workbook Tabs
   End With
   ActiveSheet.DisplayPageBreaks = True
   With Application
     .DisplayFullScreen = False                          'Sets Excel to Disable Full Screen
     .DisplayFormulaBar = True                           'UnHides Excel formula bar and toolbars
     .DisplayStatusBar = True                            'UnHides Excel Status Bar
     .CommandBars("Worksheet Menu Bar").Enabled = True   'UnHides Excel Menus
     .CommandBars("Standard").Visible = True             'Unides Standard Toolbar
     .CommandBars("Formatting").Visible = True           'UnHides Formatting Toolbar
End With
Application.DisplayFullScreen = False
ThisWorkbook.Close savechanges:=True
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
End Sub

这就是代码运行后发生的情况,并且软件无法关闭(即使没有X等,也只有在我最小化软件的情况下)

enter image description here

有可能解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

也许值得研究,请在下面查看我的评论...

Application.DisplayFullScreen = False
ThisWorkbook.Close savechanges:=True 'Here you close the workbook and save it.
Application.DisplayAlerts = False
ThisWorkbook.Save 'This part doesn't make sense..., do you need to save it again when you already closed the workbook window?
Application.DisplayAlerts = True
Application.Quit

疯狂的猜测..但也许可以。将保存部分替换为:

ActiveWorkbook.Close True
'or
ActiveWorkbook.Close SaveChanges:=1