|
-
Mar 3rd, 2006, 01:04 PM
#1
Thread Starter
New Member
[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:
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
End Sub
Last edited by Hack; Mar 3rd, 2006 at 01:10 PM.
Reason: Added [vbcode] [/vbcode] tags and for more clarity.
-
Mar 3rd, 2006, 01:07 PM
#2
Member
Re: Closing Excel with VBA
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
-
Mar 3rd, 2006, 01:10 PM
#3
Thread Starter
New Member
Re: Closing Excel with VBA
Where do I put this in my code? Do I use this exact syntax?
-
Mar 3rd, 2006, 01:11 PM
#4
Re: Closing Excel with VBA
Excel VBA question moved to Office Development
-
Mar 3rd, 2006, 01:15 PM
#5
Thread Starter
New Member
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
-
Mar 3rd, 2006, 01:15 PM
#6
Member
Re: Closing Excel with VBA
 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
-
Mar 3rd, 2006, 01:17 PM
#7
Member
Re: Closing Excel with VBA
 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
-
Mar 3rd, 2006, 01:18 PM
#8
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:
ThisWorkbook.Save
Application.Quit
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 3rd, 2006, 01:20 PM
#9
Thread Starter
New Member
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).
-
Mar 3rd, 2006, 01:23 PM
#10
Re: Closing Excel with VBA
VB 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.Save
Application.Quit
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 3rd, 2006, 01:24 PM
#11
Member
Re: Closing Excel with VBA
 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
-
Mar 3rd, 2006, 01:30 PM
#12
Thread Starter
New Member
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
-
Mar 3rd, 2006, 01:45 PM
#13
Re: Closing Excel with VBA
No, as it will only save the "ThisWorkbook" instance and not any others.
VB Code:
Public Sub CloseAll()
Dim Wb As Workbook
SaveAll
For Each Wb In Workbooks
wb.Save
Next
Application.Quit
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 3rd, 2006, 01:48 PM
#14
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 3rd, 2006, 02:02 PM
#15
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|