Closing Excel Application using VBA

前端 未结 8 986
忘了有多久
忘了有多久 2020-11-29 04:44

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

Application.ActiveWindow.Close SaveChanges:         


        
相关标签:
8条回答
  • 2020-11-29 04:56
    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
    
    0 讨论(0)
  • 2020-11-29 05:02

    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.

    The following lines work just fine:

    Sub test_t()
      Windows("yourfilename.xlsx").Activate
      ActiveWorkbook.Close SaveChanges:=False
    End Sub
    
    0 讨论(0)
  • 2020-11-29 05:06
    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...

    0 讨论(0)
  • 2020-11-29 05:06

    You can try out

    ThisWorkbook.Save
    ThisWorkbook.Saved = True
    Application.Quit
    
    0 讨论(0)
  • 2020-11-29 05:07

    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

    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.

    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.

    0 讨论(0)
  • 2020-11-29 05:07

    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
    
    0 讨论(0)
提交回复
热议问题