I have a program that runs as soon as the workbook is open using the Private Sub Workbook_Open() function located in the ThisWorkbook object. I would like to add a closing function as well, so that basically the program will run as soon as the workbook is open, and then close right away so it never stays open, just runs and closes.
I've tried adding:
Workbooks("Fire Ext. Comments EXE.xlsm").Close SaveChanges:=False
...right before the End Sub at the bottom of the program, but the workbook stays open so I'm wondering if it belongs in a different module and then called separately? Let me know what you think.
you can try this, so that the application itself will close
Application.DisplayAlerts = False
Application.Quit
Related
Long ago I had a macro that would open some workbooks, do some fiddling, then close - all fully automated, called from the commandline so I could batch file it and stick it in windows schedule to run when needed.
I've had call to do it again and of course no longer have access to those files (like four companies ago) so had a bash at recreating them, but stuck on closing excel.
ActiveWorkbook.Close SaveChanges:=True
This works to close the workbooks I'm updating, however, if I use it on the macro containing workbook, it closes the workbook within Excel, but leaves the instance of excel running (with no workbooks).
Application.Quit
This is what I think should be the right command, however this also throws an error:
Run-time error '1004':
Application-defined or object-defined error
However, it's odd. If I just run that command within excel on its own, there's no error, it only errors if the workbook calls the macro containing it.
As it's an auto-launching macro, I have an initial macro in ThisWorkbook that has the Private Sub Workbook_Open() which kicks everything off.
I'd read that Application.Quit can cause this error message when the workbook has an auto-launching macro and it's called from a module, rather than ThisWorkbook, so I moved the Sub QuitExcel() that I created, containing just Application.Quit but still get this error.
The suggested posts from SO when posting this gave a few things to try, like DisplayAlerts=False and saving the workbook, so I updated my quit sub to:
Sub QuitExcel()
Application.DisplayAlerts = False
Application.EnableEvents = False
ThisWorkbook.Save
Application.Quit
ThisWorkbook.Saved = True
End Sub
(and various permutations of that)
However I still persist in getting the same error.
Interestingly, if I click "debug" (rather than end, continue is greyed out) Excel still quits, and doesn't actually go to debugging.
Anyone have experience in what I'm trying to achieve, I just want to kill Excel after it's finished running, from auto-launching a macro?
Answer turned out to be relatively easy, I need to close my workbook before quitting excel and the closing macro can't be in the ThisWorkbook, it has to be in a module (otherwise when the workbook is closed, access to the rest of the code is gone).
So my command ended up being:
Sub QuitExcel()
ActiveWorkbook.Close SaveChanges:=False
Application.Quit
End Sub
Just needed a bit more googling and trial/error.
You should know that VBA code only run in a workbook context ThisWorkBook , that mean when you close it using WorkBook.Close, there will be no code to execute.
So, you only save then use Application.Quit
Do not close the workbook
For Each w In Application.Workbooks w.Save Next w Application.Quit
how can i close an excel file as i close the userform without closing other excel files.
i used this code but it closses all the workbooks that are running.
Private Sub CommandButton1_Click()
Unload UserForm1
Workbooks("ThisWorkbook1").Close SaveChanges:=True
End Sub
How can i fix this code,
Thank you
Unload UserForm1
That's unloading the default instance of the form, which may or may not be the instance that's currently running. Avoid referring to UserFormModuleName in the form's code-behind: use Me instead:
Unload Me
That said, if you're planning to close the containing workbook just after that, whether you unload the form or not makes little to no difference whatsoever. In fact, I would avoid Unload altogether (see this article I wrote a little while back).
Assuming you mean to close ThisWorkbook (i.e. the code that contains this VBA code), then use ThisWorkbook to qualify the Workbook.Close member call:
ThisWorkbook.Close SaveChanges:=True
That said...
Workbooks("ThisWorkbook1").Close SaveChanges:=True
That instruction can only ever close one single workbook, if it doesn't blow up with error 9 because there is no ThisWorkbook1 workbook opened - your princess is in another castle, this cannot possibly close all opened workbooks.
I would look at the code that's invoking UserForm1.Show; if there's code after the .Show call that's doing something like Application.Quit, then that's why everything is closing: assuming you've shown us all of it, the form's code-behind can't possibly be doing that.
Final workbook in a chain of 4 workbook's open and closing each other will not close via VBA macro when run in the chain (but works fine when run alone and the others close fine).
I tried all these codes (which all work, when I open just one file, but not when its the last workbook). No errors or dialogue boxes pop up, it seems to complete smoothly in that sense, however the window remains open. No close action done.
Windows("WorkbookName.xlsm").Close
Workbooks("WorkbookName.xlsm").Close SaveChanges:=False
If ActiveCell.Value = "Close this" Then
ThisWorkbook.Saved = False
ThisWorkbook.Close
End If
Call WorkbookClose
(tried all three above in a separate sub and called it)
The process for this is pretty much along these lines..
Workbook opens and auto runs a private sub which calls a macro in the workbook via "Run MacroName" which does like open a CSV, resave as XLSX.
Opens them via
Workbooks.Open Filename:= _ (directory) (This works)
Then the main workbook is opened and follows these steps
ThisWorkbook > Private Sub > Calls macro
The macro called, closes the last workbook via:
Windows("WorkbookName.xlsm").Close
'This works
Runs some simple code which opens another file, saves as XLSX, closes it.
Opens another file which does the same thing i.e. auto runs and closes previous workbook, runs a bit of code, saves, opens another workbook which closes the last. However when it reaches the fourth workbook (the last one) it stops working. The code does not close that workbook.
Also to mention if I add any code after these lines, they execute. Its as if Excel just skips the close commands without error and keeps going until the end of the code.
Windows("WorkbookName.xlsm").Close
Workbooks("WorkbookName.xlsm").Close SaveChanges:=False
If ActiveCell.Value = "Close this" Then
ThisWorkbook.Saved = False
ThisWorkbook.Close
End If
Call WorkbookClose
No error messages pop up.
I just want the final workbook to close.
From my add-in file named "myAddIn.xlam" I run the follwoing sub
sub fisrtSub()
'I use any variable name so I can call application.run
anything = Application.Run("FileOfSecondSub.xlsm!secondSub",arg1,arg2)
end dub
So vba goes to secondSub on the workbook named "FileOfSecondSub.xlsm"
sub secondSub()
Workbooks("myAddIn.xlam").Close
msgbox "Success"
end sub
After closing my add-in workbook VBA stop the execution of the code without showing any message, I ve tried to using error handler techniques but the code still stops just after I close my add-in file. Is it possible to keep the sub running after I close the add-in file (the first file that started running the subs)?
And no, I can't close the workbook myAddIn.xlam latter on the code since I need to close it to replace myAddIn.xlam for a new one.
It looks like it won't work because you're essentially cutting the head off your code. When you close the file that kicked off your process ("parent"), the code will terminate. If you need additional code to run after the "parent" workbook is closed, try writing that piece into a VBScript file. Then, call the script just prior to closing the "parent" file.
I keep running into an issue where the worksheet in question gets updated, and then doesn't reflect the changes until I close and re-open the book. It's not really a big deal, I just want to simplify it a bit into a single button click.
I was told that this is impossible to do because closing a workbook will immediately stop execution of code, so you can't reopen the book. But since, I have learned about the PERSONAL.xlsb, and figured since it's a persistent workbook, it could handle the code execution to save, close, and reopen the workbook.
Basically, here's what I've got in the main worksheet:
Public Sub Refresh()
ActiveWorkbook.Save
Application.Run "PERSONAL.xlsb!Module1.RefreshCurrentSheet",ThisWorkbook.Name,_
ThisWorkbook.FullName
End Sub
Which then calls the personal.xlsb macro "Refreshcurrentsheet":
Private Sub RefreshCurrentSheet(ByVal sheetname, Optional ByVal sheetfullname = 0)
Workbooks(sheetname).Close
MsgBox "So far so good!" 'I never see this box
Workbooks(sheetfullname).Open
MsgBox "No errors here..." 'I never see this box
End Sub
This successfully closes the workbook in question, but code execution stops and it doesn't reopen. Is there a way around this? Some way to run the second macro in the persistent sheet without code execution stopping?
We can reopen the workbook using this code inside it (there is no need to use other workbook):
Sub test()
Application.DisplayAlerts = False
Workbooks.Open ThisWorkbook.FullName
Application.DisplayAlerts = True
End Sub