Excel VBA Screen Updating
VBA Screen Updating is a property used to avoid or prevent distraction flashes while running the code and make it fast by turning off Screen Updating. We can turn off the screen updating by setting this property as false.
Often times we can feel the excel screen goes crazy while the macro is running, and we almost get frustrated by that. But how do we deal with these situations and make the code run faster than the usual slow thing?
Screen Updating is something we can notice while the excel macro is running. When the task is executing, we can notice our screen is updating the values until the macro finishes its assigned task. As our screen flickering or refreshing, it leads to the slowdown of the excel program and takes a longer time than usual to complete the task.
In VBA, we have a property called “ScreenUpdating,” and we set this property to FALSE so that it will eliminate the process of screen updating while the code is running.
In this article, we will say goodbye to the watching of on-screen action drama while the code is running. Today you will make your code run faster and quicker than your usual time.
When to use Screen Updating Feature?
Suppose you have any doubt when to use this technique. Look into the below points.
- When you are looping through a large number of cells.
- Sending emails from Excel VBA.
- Switching between excel workbooks.
- Opening new workbooks.
How to use Screen Updating Feature in VBA Code?
Example #1 – Turn Off Screen Updating
For example, look at the below code.
Sub Screen_Updating() Dim RowCount As Long Dim ColumnCount As Long Dim MyNumber As Long MyNumber = 0 For RowCount = 1 To 50 For ColumnCount = 1 To 50 MyNumber = MyNumber + 1 Cells(RowCount, ColumnCount).Select Cells(RowCount, ColumnCount).Value = MyNumber Next ColumnCount Next RowCount End Sub
The above has a nested VBA loop to insert serial numbers from the first column to the 50th column and again comes back and insert serial number starting from 51 from the second row to the 50th column.
4.9 (1,353 ratings) 35+ Courses | 120+ Hours | Full Lifetime Access | Certificate of Completion
Like this, it will insert until it reaches the 50th row.
While this code is running, you can notice your screen flickering, and you cannot do anything apart from watching this crazy moment.
To avoid all of these, we can add Screen Updating to FALSE.
To access the Screen Updating feature first, we need to access the Application object.
As we can see with the Application object, we have many properties and methods. So, select Screen Updating from the IntelliSense list.
Note: You have to apply the Screen Updating feature immediately after the declaration of the variables.
After selecting the Screen Updating property, put an equal sign (=).
As we can see, two Boolean values, i.e., FALSE & TRUE.
To stop screen updating, set the status to FALSE.
Now, as the macro starts running first, it will update the screen updating status to FALSE and proceed to the next line.
Since macro executed Screen Updating to FALSE, it will not allow the screen to update while the code is executing its task.
Example #2 –
Always Set Screen Updating to TRUE at the End
I have seen many people set the Screen Updating to FALSE but forgot to set it back to TRUE at the end of the macro.
Always set the Screen Updating back to TRUE at the end of the macro.
Sub Screen_Updating() Dim RowCount As Long Dim ColumnCount As Long Dim MyNumber As Long Application.ScreenUpdating = False MyNumber = 0 For RowCount = 1 To 50 For ColumnCount = 1 To 50 MyNumber = MyNumber + 1 Cells(RowCount, ColumnCount).Select Cells(RowCount, ColumnCount).Value = MyNumber Next ColumnCount Next RowCount Application.ScreenUpdating = True End Sub
This has been a guide to VBA Screen Updating. Here we discuss how to use Application.ScreenUpdating feature to make code run faster and quicker than your usual time along with examples and downloadable excel template. Below are some useful excel articles related to VBA –