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.
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
I'm trying to make a code that gives a message (or run a different code) on opening the workbook.
The file is shared through the old method of Excel (works better and faster) on a network folder. When im opening the file the message is given correctly, when someone else on our network is opening the same file he is not getting the message. Any idea why this is?
This is my code (in ThisWorkbook section)
Private Sub Workbook_Open()
MsgBox "Check for update complete"
End Sub
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.
Excel 2016 (or 365) does not seem to fire the Workbook_Open() sub reliably or more precisely, not at all!
The simple event sub
Private Sub Workbook_Open()
MsgBox "Work book is open"
End Sub
does not seem to work. However, if a workbook is already open and then the workbook containing the above Sub is then opened, it does run as expected.
I notice that unlike Excel 2010, 2016 (365) opens each workbook in its own window, not a workbook window in the Excel application window. Is this a bug in 2016 and is there a workaround?
I have produced a work around for my own applications and that is call the activation of a worksheet and call my initialization routines from there. But a bit "rough" and it would be good to have the Workbook_Open() sub working correctly.
It is a simple single Sub in the ThisWorkbook module. Macros are enabled. In Excel 2010 it works perfectly, as do two other macros in other workbooks for which I have written macros. It is just this one thing with Excel 2016. Given that the Workbook_Open() sub is the gateway to a workbook it seems a little strange that we have to go to a workaround to make it function.
Try encapsulating the call with a new instance of Excel. Example below:
Sub OpenInNewExcel()
Dim Background_Excel As Excel.Application
Dim pathName As String
Dim fileName As String
Let pathName = "Enter your path here" 'include "\" at the end
Let fileName = "Enter your file name here"
Background_Excel.Workbooks.Open fileName:=pathName & fileName
Background_Excel.Parent.Quit ' This is how you close the file completely using VBA otherwise the file will close and the Excel Shell will remain.
End Sub
Also make sure that enable macros is turned on in the Options-Trust Center.
You have to add the file/folder location of your workbook as a "Trusted Location".
You can see more info about that in Designate trusted locations for files in Office 2016.
I have same problem then I found solution after google it:
https://www.myonlinetraininghub.com/excel-forum/vba-macros/excel-2016-workbook_open-event-doesnt-trigger
Then I also use "Private Sub Workbook_Open()" and "Public Sub Auto_Open()" open in excel 2016 that work fine:
Private Sub Workbook_Open()
CustomStartUp
End Sub
Public Sub Auto_Open()
CustomStartUp
End Sub
Private Sub CustomStartUp()
MsgBox "Work book is open"
End Sub
I've had this problem (I'm using Microsoft 365), and I found this thread.
It happens for me sometimes when I have another workbook already open, then, on trying to open my macro-enabled workbook, before any sheet is displayed I get the Microsoft warning message about macros. Then, although I click its 'Enable' button, the Workbook opens, macros do get enabled, but Workbook_Open doesn't run.
I've never known the problem to occur if no other workbook is open. (Of course, the user might still get the yellow-backed messages at the top of the workbook, asking them to click the Enable Editing and/or Enable Macros buttons.)
Note that my Workbook_Open just calls another 'workbook-opening' sub in a module to do all the opening processing.
My solution: When my workbook-opening sub is called, it sets a global variable to True to indicate it has run.
I've made it obvious to the user that the problem has occurred, by means of a 'Welcome' sheet with all its cells locked, so the user can do nothing; at this point all other sheets are very hidden. The workbook-opening sub, when it runs, deactivates this sheet and makes it very hidden, so the user never normally sees it, and makes the other sheets visible. But if this screen remains, it instructs the user to select the other workbook, then select this one again. My Workbook_Activate code then runs, and because the global variable isn't True, it calls the workbook-opening sub. If this global variable is True, it does nothing.
To make this work, the Workbook_Close sub makes the other sheets very hidden and the Welcome sheet visible, ready for the next time the Workbook is opened.
Hey presto, the problem is solved.
The Welcome sheet actually has a dual purpose, in that if either of the yellow-backed warning messages are displayed, it will remain and force the user, with suitable instructions, to click Enable Editing and/or Enable macros. If the users aren't au fait with macro-enabled Excel, they will just ignore these and try to carry on regardless.
All this is much easier to implement than to explain. I hope it's clear.
And I hope this might be of help to someone.
I had this issue with one of my files as well. I managed to fix this issue by running Workbook_Open manually in the VBA editor once open and saving the file in another location. The file in the new location should have no issue with auto-running Workbook_Open. If this doesn't work, copy the original file to a new location before manually running & saving.
If the newly saved file does not run Workbook_Open, repair your version of Office.
I am building a database in Access, for which I import data from an Excel workbook questionnaire. I have coded an Import-sub that selects, opens, retrieves the data from and finally closes the workbook.
The problem is this: for some reason, when I now open any excel workbook on my computer (at a time when neither Access or Excel is in use) some old version of the questionnaire keeps opening as well. This problem doesn't end by restarting the computer, but only by deleting that specific questionnaire-workbook. But then it starts happening with another workbook.
I have a theory that this might be because I - in my import-sub - have opened the questionnaire, encountered a run-time error which has ended the sub before it closed the workbook, and that somehow the workbook is still "open". Or that there still is a link active.
So I have two questions:
1.) Does anyone know how I can fix this problem?
2.) Is there generally any consequences of not closing a workbook that you open through VBA?
My relevant code is:
Dim MyXL As Excel.Application
Dim MyWB As Excel.Workbook
...
in between lots of stuff happening, several times an error occurs which interrupts the program.
...
MyWB.Close False
MyXL.Quit
Appreciate any help on this!
I did Encounter the same Problem and found out that Excel stores the files that open whenever you start Excel in a Folder (XLSTART). The path to mine was: c:\USERS\MyUserName\AppData\Roaming\Microsoft\Excel\XLSTART
As suggested by Ross McConeghy error handling may prevent such an occurrence. But the error already happened and the questionnaire, as you suggested, has placed that workbook in the Folder XLSTART. You have to delete it from that folder to fix the unwanted occurrence.
Your theory is likely. If you never display the Excel application, errors are encountered, and your code never closes the workbook it would be left open in the background and your code would most likely lose reference to it. The next time you open an Excel file the instance of Excel that is already open(but hidden from view) "picks up" the request and opens the file, also displaying the file that was still open from the macro.
1.) You should set up error handling so that the Workbook and Excel application are closed when there is a non-recoverable error.
Sub MySub()
On Error GoTo handle_Err
<... code ...>
Exit Sub
handle_Err:
MyWB.Close False
MyXL.Quit
End Sub
If you have other Error handling statements in your code always reset to On Error GoTo handle_Err instead of GoTo 0 etc.
2.) The only consequences of not closing the Workbook are the obvious ones- the system resources it's using and, if it is open for editing, no one else can edit it.
I had a similar problem and solved it a different way.
I found the connection to an external workbook and fixed it by going to Data > Edit Links.