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.
Related
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.
Is there a way to put a breakpoint on ActiveWindow.change ? I have a macro that I run in a new, unsaved Excel file, that is supposed to open CSV file, save it as, process it, and keep it in focus. Instead of that, when the macro is done it selects the empty new file that was unsaved, and puts the CSV in the background. This happens even though I use the code below, just before End Sub:
WKB1.Activate
WKB1.Sheets(1).Activate
WKB1.Sheets(1).Range("A1").Select
The thing is the three rows of code above do set the focus correctly, but at end sub it switches back to the unsaved file.
I was thinking that if I can set a breakpoint whenever the active window name changes, I can catch when that happens, because the macro I'm working with is huge, and I can't find the bug manually.
Kind regards,
Daniel
The fix was quite stupid, but I fixed the issue by creating a new ribbon tab and run the macro from the ribbon instead of running it from VBA. Also i removed and Activate o Select commands. The macro was behaving the same, even after i removed the Activate and Select commands, the thing that fixed it was running it from a new menu.
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.
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?
In 2003 the workbook would just close. But now the same code is re-opening the workbook. There are some lines afterwards and then the sub ends. When the sub ends, the workbook_open event gets fired for the closed workbook, even though there is no code that opens any workbooks. The debugger is almost useless, it is not reproducing the bug if I execute everything step by step, in fact everything works fine when I use the debugger.
I use
ActiveWorkbook.Close False
For closing the workbook (from an add-in).
Help much appreciated.
I added a slight amount of code for Excel 2007 because of the annoying ribbon and Microsoft getting rid of custom menus. To somewhat compensate for this, I wanted to have the add-ins tab in the ribbon always visible for convenience, but I was using Application.OnTime and sendkeys to do it (because Microsoft didn't bother including an API with the stupid ribbon...). Well OnTime seems to have been the root of my troubles.
Figured out how to get both. Workbook_beforeclose cancels the scheduled ontime event by calling:
Application.OnTime EarliestTime:=Now(), Procedure:="Name", Schedule:=False
but otherwise the ontime event gets executed.
If the event does not get cancelled before beforeclose is called, the workbook will close then re-open to run the scheduled ontime event (because the workbook is opened so briefly that ontime doesn't get a chance to run before I need to close it).