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.
Related
I work with a a group of six interlinked Excel workbooks that all feed into one summary/reporting workbook (let's call it "Main Report.xlsm").
Since the various workbooks are updated with new information throughout the day as it comes to hand, they need to be saved regularly throughout the day. I have therefore created a simple VBA macro that saves all open workbooks at the press of a button, which I have located as on object on Sheet1 of MainReport.xlsm. The macro works OK and saves all open workbooks as expected, however two aspects of the code are not working as expected:
No matter where I insert ScrenUpdating = FALSE, Excel still displays each workbook on the screen as it saves it. I don't want that because it slows down the save process, plus it looks weird as the macro toggels through each open workbook and the workbooks flash up momentarily on the screen. Since I activate the save macro from Main Report.xlsm, I want the screen to freeze on that workbook whilst Excel saves all open workbooks as a 'background' process. Can anyone tell me how I can make ScreenUpdating work properly with this code?
At the end the macro is supposed to select the 'Main Report.xlsm' workbook, but instead of activating that workbook, Excel seems to get stuck on displaying the second-last workbook in the group of six open workbooks. If I test the code by stepping through it, it seems to work OK, but when I run it from the assigned button on Main Report.xlsm, it does not work. Why would this be happening?
Here is my simple 'SaveAll' macro VBA code:
Sub SaveAll()
Application.ScreenUpdating = False
Dim Wkb As Workbook
Application.Calculate
For Each Wkb In Workbooks
If Not Wkb.ReadOnly And Windows(Wkb.Name).Visible Then
Wkb.Save
End If
Next
Application.Workbooks("Main Report.xlsm").Activate
ActiveWorkbook.Worksheets("Sheet1").Select
ActiveSheet.Range("C4").Select
Application.ScreenUpdating = True
End Sub
Can anyone spot what I am doing wrong?
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 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
Under this line of code, Workbooks.Open(previous_absolute_path) freezes Excel most of the time, giving no error message:
Set current_workbook = Workbooks(get_file_name(current_absolute_path))
The surounding code:
If IsWorkBookOpen((current_absolute_path)) = False Then
If previous_absolute_path <> "" And previous_absolute_path <> current_absolute_path Then
Workbooks(get_file_name(previous_absolute_path)).Close True
End If
Set current_workbook = Workbooks.Open(current_absolute_path)
previous_absolute_path = current_absolute_path
Else
Set current_workbook = Workbooks(get_file_name(current_absolute_path)) 'Causes Fail!
End If
current_absolute_path is a valid file path that is absolute. It seems every time the code runs there are windows at least being opened. However, in the newly opened workbook, no lines of code are ever executed in itsworkbook_open() from what I can tell from putting a message box as the first line of code ofworkbook_open() and the message box never appears.
The code does work, if and only if I have the workbook running this macro open, and before I run that macro, I manually open the workbook at current_absolute_path. I close that file and run the macro and it works without any problems.
current_absolute_path, is a confirmed valid file path. The line of code can open other workbooks within the same folder. Excel allows all macros and "trusts" files from the internet at this point. The folder of the workbook being opened is not protected.. and I do not have the skills yet to know where the problem is. What must be done?
EDIT: It's not manually opening the workbook that causes the Excel macro to run successfully the next time, specifically its manually closing it.
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.