Speed Up Macros

How to optimize your macros to run faster. Watch the video below:

Speed Up Macros
  1. Turn Off Screen Updating:

To turn off screen updating, use the following statement:

Application.ScreenUpdating = False

If we wish the user to see what action the macro is performing at a specific time, we can use the following statement to turn screen updating back on:

Application.ScreenUpdating = True

2. Turn Off Automatic calculations:

Example:
Sub No_Automatic_Calculations()
Application.Calculation = xlCalculationManual
‘…Other Lines of Code
Application.Calculation = xlCalculationAutomatic
End Sub

3. Disable display alerts

Example – Refer to the video

Sub FillRangeWithNumbers()

Dim row As Long, col As Long
Dim Number As Long
Number = 0
For row = 1 To 60
For col = 1 To 60
Number = Number + 1
Cells(row, col).Select
Cells(row, col).Value = Number
Next col
Next row
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True

End Sub

4. Simplify Object References
Instead of
Workbooks(“MyBook.xlsx”).Worksheets(“Sheet1”).Range(“Z2”)
We can write:
Set GST = Workbooks(“MyBook.xlsx”).Worksheets(“Sheet1”).Range(“Z2”)

GST.Value=0.18

5. Declaring Variable Types – each of the variables is declared with its data type.

Dim I as Long

Dim MyName as String

Dim Employees() as String

6. Use ‘WITH’ Statement

Instead of
Sub No_WITH()
Worksheets(“Sheet1″).Range(“B1”).Value = 500
Worksheets(“Sheet1″).Range(“B1”).Font.Bold = True
End Sub
We code like this:
Sub Use_WITH()
Set ws = ThisWorkbook.Worksheets(1)
With ws.Range(“B1″)
.Value = 500
.Font.Bold = True
End With
End Sub

7. Copy and Paste

Best is to use the following approach instead of copy, select and paste:
Sub CopyPaste()
Sheets(“Sheet1”).Range(“B1:F10”).Copy Destination:=Sheets(“Sheet2”).Range(“B1”)
End Sub

8. Using VBNullString for Placing Blanks in Worksheet Cells is faster than using “” Double Quotes.

Speed up macros by setting display alerts to false
Display Alerts Set to TRUE

The above are some of the simple steps that we can undertake to make our macros or VBA code execute faster.