Results 1 to 15 of 15

Thread: [RESOLVED] Closing Excel with VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    6

    Resolved [RESOLVED] Closing Excel with VBA

    I need some help on this, I have looked around and found some posts, but I can't get it to work.

    I am opening an Excel file with windows scheduler on our server. Once the file is open, it automatically updates and I have written a VBA macro to save and close it. My problem is that Excel itself does not close, just the workbook closes. How do I get Excel to fully close?

    Here is my vba... Thanks!!
    VB Code:
    1. Public Sub CloseAll()
    2.  
    3. Dim Wb As Workbook
    4. SaveAll
    5. For Each Wb In Workbooks
    6.     If Wb.Name <> ThisWorkbook.Name Then
    7.         Wb.Close savechanges:=True
    8.         Set Wb = Nothing
    9.     End If
    10.    
    11. Next Wb
    12. ThisWorkbook.Close savechanges:=True
    13. Set Wb = Nothing
    14.  
    15. End Sub
    Last edited by Hack; Mar 3rd, 2006 at 01:10 PM. Reason: Added [vbcode] [/vbcode] tags and for more clarity.

  2. #2
    Member Sahir's Avatar
    Join Date
    Jan 2006
    Posts
    38

    Re: Closing Excel with VBA

    Application.Quit
    Yo soy yo y mi circunstancia - José Ortega y Gasset
    Und wenn du lange in einen Abgrund blickst, blickt der Abgrund auch in dich hinein - Friedrich Nietzsche

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    6

    Re: Closing Excel with VBA

    Where do I put this in my code? Do I use this exact syntax?

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Closing Excel with VBA

    Excel VBA question moved to Office Development

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    6

    Re: Closing Excel with VBA

    Ok, I put the application.quit at the bottom of my code. It still does not work, I am 2 day newbie at VBA, help?!

    VBA code...

    Public Sub CloseAll()

    Dim Wb As Workbook
    SaveAll
    For Each Wb In Workbooks
    If Wb.Name <> ThisWorkbook.Name Then
    Wb.Close savechanges:=True
    Set Wb = Nothing
    End If
    Next Wb
    ThisWorkbook.Close savechanges:=True
    Set Wb = Nothing

    application.quit

    End Sub

  6. #6
    Member Sahir's Avatar
    Join Date
    Jan 2006
    Posts
    38

    Re: Closing Excel with VBA

    Quote Originally Posted by yarsec
    Where do I put this in my code? Do I use this exact syntax?
    Yes you use the exact syntax. You can call Application.Quit as soon as you are done with closing the workbook.
    Yo soy yo y mi circunstancia - José Ortega y Gasset
    Und wenn du lange in einen Abgrund blickst, blickt der Abgrund auch in dich hinein - Friedrich Nietzsche

  7. #7
    Member Sahir's Avatar
    Join Date
    Jan 2006
    Posts
    38

    Re: Closing Excel with VBA

    Quote Originally Posted by yarsec
    Ok, I put the application.quit at the bottom of my code. It still does not work, I am 2 day newbie at VBA, help?!
    Where is this code? Is it in an Excel workbook or somewhere else?
    Yo soy yo y mi circunstancia - José Ortega y Gasset
    Und wenn du lange in einen Abgrund blickst, blickt der Abgrund auch in dich hinein - Friedrich Nietzsche

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Closing Excel with VBA

    You you cant close the current workbook and still try to execute vba code as its no longer open. Save the current workbook instead of closing it. Then quit the application.
    VB Code:
    1. ThisWorkbook.Save
    2.     Application.Quit
    3. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    6

    Re: Closing Excel with VBA

    I think its in an Excel workbook. From Excel I went to Tools-> Macro -> Visual Basic Editor and inserted two modules (saveall and closeall).

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Closing Excel with VBA

    VB Code:
    1. Public Sub CloseAll()
    2.  
    3. Dim Wb As Workbook
    4. SaveAll
    5. For Each Wb In Workbooks
    6.     If Wb.Name <> ThisWorkbook.Name Then
    7.         Wb.Close savechanges:=True
    8.         Set Wb = Nothing
    9.     End If
    10.    
    11. Next Wb
    12. ThisWorkbook.Save
    13. Application.Quit
    14.  
    15. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    Member Sahir's Avatar
    Join Date
    Jan 2006
    Posts
    38

    Re: Closing Excel with VBA

    Quote Originally Posted by yarsec
    I think its in an Excel workbook. From Excel I went to Tools-> Macro -> Visual Basic Editor and inserted two modules (saveall and closeall).
    If it's in an Excel workbook it should work. Anyway, I am off to bed now. Good night.
    Yo soy yo y mi circunstancia - José Ortega y Gasset
    Und wenn du lange in einen Abgrund blickst, blickt der Abgrund auch in dich hinein - Friedrich Nietzsche

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    6

    Re: Closing Excel with VBA

    Robdog:

    What about this? It seems to work and its simple.

    Inserted just 1 module:

    Public Sub CloseAll()

    ThisWorkbook.Save
    Application.Quit

    End Sub

    Then put the following into Thisnotebook...

    Private Sub Workbook_Open()
    Run "CloseAll"
    End Sub

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Closing Excel with VBA

    No, as it will only save the "ThisWorkbook" instance and not any others.
    VB Code:
    1. Public Sub CloseAll()
    2.  
    3. Dim Wb As Workbook
    4. SaveAll
    5. For Each Wb In Workbooks
    6.     wb.Save
    7. Next
    8. Application.Quit
    9. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Closing Excel with VBA

    Why do you need it to save and close all workbooks upon the Workbook_Open event?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    6

    Re: Closing Excel with VBA

    Thanks! It looks like its working!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width