Table Of Contents
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."
We often feel the Excel screen going crazy while the Macro is running. 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 notice our screen updating the values until the Macro finishes its assigned task. As our screen flickers or refreshes, 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," we set this property to FALSE to eliminate the screen updating process while the code runs.
This article will say goodbye to watching on-screen action drama while the code is running. Instead, 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 about when to use this technique. Then, look into the below points.
- Looping through a large number of cells.
- Sending emails from Excel VBA.
- Switching between Excel workbooks.
- Opening new workbooks.
How to use the Screen Updating Feature in VBA Code?
Example #1 - Turn Off Screen Updating
Look at the below code.
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 to insert serial numbers starting from 51 from the second row to the 50th column.
Like this, it will insert until it reaches the 50th row.
While this code is running, you can notice your screen flickering. 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 must apply the Screen Updating feature immediately after declaring the variables.
After selecting the Screen Updating property, put an equal sign (=).
As we can see, there are two Boolean values: FALSE and TRUE.
To stop screen updating, set the status to FALSE.
As the Macro starts running first, it will update the screen, updating the 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
We 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.
Code:
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