Excel does not completely close when using Workbook.Close - vba

In the macro I'm running, I end with closing the last Excel Workbook that's open. When I run it everything closes except one window which doesn't have a spread sheet, just a light-blue backdrop. Am I using a wrong command or is there just no way to close all open windows?
Here is a post asking my exact question, but the solution isn't working for me.
Here are the last two commands that I'm using:
'
' Closes Populated JD Form
'
Workbooks(name).Close SaveChanges:=False
'
' Closes Transfer Template
'
Workbooks("Transfer Template.xlsm").Close SaveChanges:=False
Thanks in advance!!!

Don't turn the display alerts off as it will suppress any genuine questions.
Use this instead
ActiveWorkbook.Saved = True
Application.Quit
This will stop Excel for asking about saving changes as it now thinks that it has been saved already. A good example of this is your personal macro workbook, I use mine a LOT and am always creating or modifying code in there. If I turn the alert off and there are changes in my personal workbook, I lose them all.
Try not to bulk fix possible scenarios where possible. Another example is errors, have an error trapping routine and deal with it as needed as opposed to a blanket statement of on error resume next.

To make sure the question doesn't remain unanswered, using both suggestions
(credit to #DeanOC and #user1274820)
'Closes Populated JD Form
Workbooks(Name).Close SaveChanges:=False
'Closes Transfer Template
Workbooks("Transfer Template.xlsm").Close SaveChanges:=False
With Application
.DisplayAlerts = False
.Quit
End With

Once I removed atpvbaen.xls from my references, application.quit worked as advertised.
I was tearing my hair out - until I realized I had atpvbaen.xls set as a reference - which was ANOTHER excel thread left alive. I didn't need it for this application, so I disabled it. In the VB Editor, Tools => References un-check atpvbaen.xls. If I had needed it, maybe shutting that one down deliberately would have worked.

Related

Delay in closing Powerpoint while doing close via VBA Macro

I have two macros in in a Powerpoint file in Microsoft Office Professional Plus 2016 version.
1. Does saveas work
2. Closes Powerpoint
I run these two macros one by one using PowerPoint /m command line option. It's working fine. However, few times 2 macro close call is intermittently taking more time. When I move mouse on PowerPoint window after "close" macro is called, it closes instantly. I am pretty sure first macro saved the file and finished execution successfully. The close call macro code is below. I don't want to save any work. I don't have any other Powerpoint process running. Please advice how can I speed up this? I wonder what's holding Powerpoint. Thank you.
Sub closeit()
With Application.ActivePresentation
.Saved = True
.Close
End With
Application.Quit
End Sub
The delay in macro 2's execution is most likely caused by macro 1 finishing the save. It's not possible to run two macros at once, so 2 can't run until 1 is done.
What's the logic behind two separate macros? It seems unneccessarily complicated, you could just add this code to the end of #1.

Excel Unprotects Sheet after VBA Code Crash Despite File Not Being Saved

I have an excel book used by colleagues that is protected when they open it, when testing a new feature I was developing I have encountered an odd quirk with Excel that I don't understand:
When clicking a button linked to a Macro, the VBA code begins by unprotecting the workbook, like this:
Sub ButtonClick()
Dim userrange As Variant
Dim rrow As Range
Dim teeth As Range
' unprotect sheet
ActiveSheet.Unprotect ("password")
Application.EnableEvents = False
The macro then crashes (I know why, that's not the issue here). I then press end on the error message pop up, and close excel without saving the file. When the file is reopened, the book is unprotected.
Essentially, the code crashes before it gets here:
' protect sheet
ActiveSheet.Protect ("password")
Application.EnableEvents = True
Can I ensure the excel file is still protected when reopened, even when the VBA code crashes after unprotecting it?
The reason this is an issue is that I have some existing functionality in the workbook that only works properly when the workbook is protected. So if somebody crashes the program then tries to reopen it, they cannot use it normally again without my input.
I find it odd that Excel 'saves' the fact that the workbook was unprotected, even if I close the file without saving anything. I'm aware some 'stuff' happens in the background when running a VBA code, for example the undo stack is cleared, I'm guessing something in the background is recording the fact the worksheet has been unprotected even if I don't save the file? I'd like to understand the mechanism, if anybody has an explanation of how the protection status is recorded, it would be appreciated.
As a workaround, you could ensure the file is protected at open using the Workbook_Open event (add to ThisWorkbook module). This doesn't explain how the workbook currently remains unprotected, but you should be able to circumvent that.
Private Sub Workbook_Open()
' Ensure sheets are protected at open
ActiveSheet.Protect "password"
End Sub

Check for "save changes to 'filename' prompt w/ VBA?

First off, this is not about the saveasfilename dialog box.
Is there a way to have a flag set that checks to see if the "want to save your changes to 'filename.xsls'? dialog box appears during a VBA sub ?
Basically I have a macro that copies some data into another file, displays a MsgBox then closes the file and Excel SHOULD prompt the user to confirm that the file is saved. However, I have a legacy program that locks that file sometimes, causing the prompt to not appear, and it looks like the file was saved but actually wasn't. To 'fix' it I have to close all instances of Excel and the program and start over. It doesn't happen often, but you can miss it if you aren't paying attention or someone less experienced in the process doesn't know to check to make SURE they are prompted to save.
What I'd like to know is if there's a way to have some sort of check / flag value, 1/0, true/false, etc to make sure that dialog box appears. If it doesn't then warn the user that they need to restart Excel and the other program. Basically I'm trying to catch an error that should never happen, so this might be unsolvable.
This is the dialog box I'm referring to:
Sometimes it doesn't appear because the file is locked and the VBA sub just continues on.
It would probably better to solve the other problem, but since you don't have posted it, we don't know it.
Anyway, everything you want is described here: https://support.microsoft.com/en-us/kb/213428
You can bascally do this:
Sub Auto_Close()
If ThisWorkbook.Saved = False Then
'ThisWorkbook.Save this would autosave
Application.GetSaveAsFilename 'this displays the save as dialog
End If
End Sub
I ended up doing this as a work around and it's not truly a solution to my question although I suspect there's got to be a way to do it through window classes checking or something. Thank you to everyone who took their time to chime in and offer their brainpower :)
I declared two variables for the file size before and after the sub runs. If the file sizes are equal, then a msgbox appears and says the file may have not been written correctly, or you are overwriting the existing file.
Dim ExportFileByte As Long
Dim ExportBytePostFile As Long
ExportFileByte = FileLen(ExportSourcePath)
' (sub runs here and exports a copy of some data to an .XLS file)
ExportBytePostFile = FileLen(ExportSourcePath)
If ExportFileByte = ExportBytePostFile Then MsgBox ("Error: File may have not have saved, or file is being overwritten without changes (OK)")

Microsoft Excel 2013 ScreenUpdating fails

After running a macro for a prolonged period of time the ScreenUpdating in Excel totally stops working even when the macro finishes. I never set it to false but it behaves as if it is set to false and the only way to make Excel update the screen again is to close it out and reopen it. The VBA screen works however. I have tried to run DoEvents and ScreenUpdating=True but the Excel window does not update visually (it is not frozen). Is there anyway of clearing the ScreenUpdating buffer? I have tried manually setting it to false and true to no avail.
If you want to duplicate this error use this code
Application.OnTime (Now + TimeValue("0:00:05")), "macroName", (Now + TimeValue("0:00:10")), True
then I have the code copy and paste some values and I control when the loop stops with a countdown value in cell A1 for example.
I got this issue in my Excel 2013. For me, it was caused when I loaded a userform. It was very, very strange, however. After a lot of trial and error I found that what caused it was using a Sheets("MySheet").Activate command. I moved this command from running after I had shown and closed the form, to immeiately before I show the form (i.e., immediately when user clicks the 'show form' button.) This fixed it. Try fiddling with your sheets.activate commands.

Excel VBA display dialog locked in front of screen during macro

I've looked for this everywhere in vain.
I have a macro in one Excel file (SplitTickets.xlsm) that loops through 3 separate Excel files.
When SplitTickets is opened a dialog appears with a button to launch the macro.
Since I don't want users to mess with the excel with accidental keys or clicks while the macro is running, I would like to keep the dialog on top of all the files being opened, modified and closed, and update the label displayed when the process is over.
I tried setting it up as modal but since the macro needs to select sheets, cells and ranges, the dialog being modal blocks the rest of the macro being executed.
And turning modal off makes it disappear beneath the flurry of the macro's manipulations.
How can I keep this dialog on top without turning it modal?
You can use the below code instead. I use it on regular basis when I don't want the user to interfere with the code run.
Sub Testing()
Application.Screenupdating = False
Your Code
'This is very important, or it will remain disabled.
Application.Screenupdating = true
End sub
This way the user is not aware of what is happening in the background.
Sandesh :)
Add the following line to the start of your macro:
Application.ScreenUpdating = False
This will turn off the "flurry of macro" activity you can see, so the users aren't tempted to interfere. It will also make your macro run faster, because it no longer has to redraw the screen between each step. Be sure to set it back to True at the end of the macro.
You may also want to use this option as well:
Application.Cursor = xlWait
This will turn the mouse cursor to an hourglass (or whatever "wait" cursor is set in their system) so the user knows the macro is running. To disable the hourglass, use this:
Application.Cursor = xlDefault
Beyond this, the macro should automatically prevent the user from interfering with the sheets while it is running. However, it will keep a queue of the keystrokes made while the macro is running and enter them after the macro is done.
Could you add a Formname.Show at the end of you macro to make it reappear?