This is something I have read a lot myself but couldn't find the solution.
I have a program of about 10,000 lines where I have a procedure attach to a particular command button. This calculates some complex equations.
I have started the code as usual with Application.ScreenUpdating = False and turned it to True just before ending of the main procedure. Yet just the upper part of my Excel Sheet (near the Menu Bar) keeps flickering about 10 second until the result appears. I tried disabling events, as well as turning the calculations to manual, but nothing helps. Even tried mentioning screen updating to false at the beginning of sub procedures related to main procedure.
Any suggestion regarding this will be really appreciated. Thank you!
Try adding this at the start of the sub:
Application.EnableEvents = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
And this just before the end:
Application.EnableEvents = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic
That should help. Note though, my screen also flickers sometimes when running complex macros (Excel looks like it's not responding, but it's working in the background).
Related
The function Application.ScreenUpdating = False is not working whenever switching between worksheets or workbooks in Excel. This function alone worked fine in Excel 2010, but doesn't work in later versions from what I can tell. I am now using the office 365 desktop version of excel. In these later versions, the command only prevents updating when selecting cells or doing things within a specific worksheet, but for my purposes I need a form to pull data from a second worksheet which causes flickering.
Is there a way to prevent the screen from updating/flickering with SheetB briefly when it gets activated in this macro?
Sub ActivateSheetB()
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Sheets("SheetB").Activate
End Sub
I've had the same thing happen to me forever and it's fairly annoying but from my own observation, I believe that application.screenupdating = false is still working. What I mean by that is your code is still being sped up. Other than it visually being annoying and making users think they broke excel this is still an effective method for speeding up your workbook even when switching between sheets.
Hopefully someone comes along with a better answer than mine because I'd love to know what that answer is as well xD
I have an Excel VBA code that reads the master data, load the data of each case/loop into a workbook, and then tons of calculations take place via numerous formulas (with no VBA codes). The desired result is the outputs of the calculations. The code loops through all the cases in the master data.
So here is the problem. I used multiple 'copy and paste' actions in each loop to load the data into the workbook, but the run time is way longer than I expected, causing Excel to be 'Not Responding'. Note that I already turned off automatic calculations in Excel and added the workbook Calculate trigger in the VBA code, so as to avoid updating the workbook entirely every time there is a paste action.
Could someone advise if directly setting the cells in the wb = the cells in the master data would speed up or slow down the code? Or could someone suggest a more efficient way of loading data?
I appreciate your effort before you send in your responses.
As said in the comments, not using .Select improves the speed of your macro. Also, copy paste actions are indeed more slow whith VBA and you should do assignations FromWorksheet.Range("CopyRange").Value = ToWorksheet.Range("PasteRange").Value. Also, it always helps to add these lines in the beginning:
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.DisplayAlerts = False
and reactivating them at the end:
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.DisplayStatusBar = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
There are many information on this, like this link
Long-running, high-end Excel-based applications that I developed years ago and that run beautifully in Excel 2007 and 2010 look like Amateur Hour in Excel 2013 and 2016 because Application.ScreenUpdating = False no longer works reliably.
The screen unfreezes apparently when VBA code copies a preformatted worksheet from the macro workbook into a new workbook, although other circumstances must trigger it as well.
I’ve seen the threads on this subject that recommend “fiddling with the code” or “calling the code in a subroutine”. Unfortunately, I have to maintain hundreds of Excel applications each with thousands of lines of code and hundreds of users who are about to migrate to Office 2016, so rewriting is not an option. How can I recover Excel’s former elegance?
I wanted to leave a comment but I am not allowed to do so. Without a code sample it is very dificult to understand your problem (please see https://stackoverflow.com/help/how-to-ask and edit your question appropriately.
Here are some ideas:
- Check if your code calls for code in a different procedure, maybe the Application.ScreenUpdating is turned on outside of the procedure.
- Try this at the beginning of your procedure:
Application.Calculation = xlCalculationManual
Then, at the end of the code set it to:
Application.Calculation = xlCalculationAutomatic
It might help; however, without a code sample it is very difficult to properly help you.
Here is a technique that helps reduce flickering and preserves the StatusBar message.
Application.Cursor = xlWait
Application.ScreenUpdating = False
. . .
Set wkbNewBook = Workbooks.Add
ThisWorkbook.Windows(1).Visible = False
. . .
ThisWorkbook.Windows(1).Visible = True
wkbNewBook.Activate
Application.ScreenUpdating = True
Application.Cursor = xlDefault
We have been dealing with this problem now for a long time as my tools do show live animated charts which all of the sudden were completely static - we would have died for having at least a flickering animation.
Initially we tried to force the animation with a forced screenupdate but that did not work. Just by pure coincidence (copy pasted too many times) we stumbled into a solution which is equally unbelievable as it does seem to work. After each Application.ScreenUpdating = True we have added x3 times DoEvents. On some systems x2 times DoEvents works but x3 times does seem to be more reliable on the various office releases out there. Voila our animation came back :-)
Application.ScreenUpdating = True
DoEvents
DoEvents
DoEvents
We have not used it for the Application.ScreenUpdating = False statement but it might do some magic there. Anyway we hope that this road can help some of you finding creative functional solutions!
After looking through many forums, I believe the flicker problem is related to SDI vs MDI.
Someone suggested setting the application to not visible.
Application.Visible=False
enter code here
Application.Visible=True
This solved my flicker problem, but I didn't like how the excel application disappeared completely then suddenly reappeared for the user.
I was able to solve the issue to my liking by using a workaround this 'which window is on top' problem.
By leaving the main window alone, and forcing other workbooks to become not visible, letting the code run, then bringing them back to visible, it stopped flickering.
Application.Workbooks("yourworkbooktohide").Windows(1).Visible = False
Just remember to bring it back with =true.
Again, my script worked just fine in Excel 2010, but after "upgrading" to 2013, this flicker issue started.
For me, only "Application.ScreenUpdating = False" did not completely cure the flickering.
Calculation caused also the flickering.
Adding "Application.Calculation = xlCalculationManual" solved the flickering issue.
So, the code should be like:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
... inportant code here....
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Nico Mijnster.
I had the same problem and, believe it or not, it was solved by unplugging and reconnecting the cable of my second monitor. This cable is using a HDMI to VGA converter.
I moved a pagebreak in an Excel sheet by hand and recorded the following macro in the process because I want to automate it:
Sub RecordedMacro()
'
' RecordedMacro Macro
'
Set ActiveSheet.HPageBreaks(1).Location = Range("A71")
End Sub
Running this recorded macro fails with an error 1004 ("application-defined or object-defined error"), even with an active sheet where I can do it manually without any problems.
I did some Internet searching and it appears that most people end up using HPageBreaks.Add instead of changing a pagebreak's location, but I'd like to know why this macro fails and if there is a way to make it work as I see no reason for throwing an error.
EDIT: The following line fails with an 1004, too:
Set ActiveSheet.HPageBreaks(1).Location = ActiveSheet.HPageBreaks(1).Location
My answer is slightly different than what you discovered, and doesn't require the ResetAllPageBreaks. In addition this one toggles ScreenUpdating to avoid the flicker of going back and forth:
Sub MoveThatHBreak()
Application.ScreenUpdating = False
ActiveWindow.View = xlPageBreakPreview
Set ActiveSheet.HPageBreaks(1).Location = Range("A11")
ActiveWindow.View = xlNormalView
Application.ScreenUpdating = True
End Sub
This is in Excel 2016. I stumbled on this answer because I could only record the move while in Page Break Preview, so figured maybe the same was true in VBA.
I found the solution. I tried to think what the difference between manually setting the pagebreak and setting it via VBA is in my case, and for reasons I do not fully understand I really have to switch the view first:
Sheets(sheetName).Activate
ActiveWindow.View = xlPageBreakPreview
Sheets(sheetName).ResetAllPageBreaks
Set Sheets(sheetName).HPageBreaks(1).Location = ActiveSheet.Range("A71")
ActiveWindow.View = xlNormalView
This makes not that much sense to me as all other operations on the HPageBreaks collection work without changing the view first, but if Excel requires it, I'll do it :).
I'm new to VBA so there might be a simple answer to this question but if there is I sure haven't found it. What I am doing is copying data from several workbooks into one master workbook. I have writen the code for this and it works fine. The only problem is the workbooks where I'm retriving the data have userforms that automatically initiate when the workbook is accesed. This means that when I run my code to copy the data it hangs at each userform and wont continue until I've physically closed each userform. So my question is: Is there a way to remotely close the userforms in the raw data workbooks from my master workbook VBA code? Thanks in advance.
to close all userforms, (if you want a specific one , change my code)
sub Close_Userforms()
Dim Form as VBA.Userform 'if not work change to Object
For each Form in VBA.Userform
'can add a condition, like : if Form.name ="Whatever" then
unload Form 'if you don't want to lose the data from the userforms, Form.Hide, and later re-loop and Form.Show
next Form
edit : can also if Typename (Form)="Whatever" then , for the condition
Assuming you mean that the forms pop up when you open the workbooks, disable events before doing so:
Application.Enableevents = False
Workbooks.Open ...
Application.Enableevents = True
for example.
I would suggest trying
Application.EnableEvents = False
Further reading.
Short description: All events (Workbook_Open, Workbook_BeforeSave etc), that usually fires upon opening or closing a workbook, will be ignored.
I have written the following functions to make all macros a bit simpler (and faster). Simply place these functions in a regular module.
Public Function CalcOff()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = False
End Function
Public Function CalcOn()
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Function
Begin your macro with:
CalcOff
And then end your macro with:
CalcOn
Remember that you need to put "CalcOn" in all places that exits the running macro.
Disabling ScreenUpdating makes the code "run in background" (nothing will be displayed).
Setting Calculation to manual improves the speed of the code, since no calculations will be made when changing data. But it's very important to exit all macros with "CalcOn", otherwise your sheet won't calculate (and that's not funny), and it will look like Excel has frozen (since ScreenUpdating would still be turned off).
However, if you by any chance happen to break a running code without exiting it the proper way (running "CalcOn"), simply close the Excel application and reopen it. Or run a macro that ends with the "CalcOn" code. Or create a new macro with that simple line.