Improving the Speed of Macros in Microsoft Excel
This article provides insights on optimizing the performance of macros to run faster in Microsoft Excel.
Improving the Speed of Macros in Microsoft Excel
Improving the Speed of Macros in Microsoft Excel. Let’s illustrate this with an example: Suppose you are working with lengthy VBA codes for calculations or other purposes, and you notice that your macro takes a considerable amount of time to execute. The following example demonstrates how you can create macros that run faster than regular code.
Suppose you need to multiply the number in column A by 10% and store the result in the corresponding cell in column B. Follow these steps to launch the VB editor:
- Click on the “Developer” tab.
- From the “Code” group, select “Visual Basic” or press ALT + F11 to open the VB Editor screen.
- Click on “Insert” and then choose “Module” to create a new module.
- Enter the following code in the module to obtain the desired result:
Sub SlowMacro()
For x = 2 To 50000
Cells(x, 1) = x
Cells(x, 2) = x + (x * 0.1)
Next x
End Sub
To enhance the speed of the macro, use the following lines of code:
Sub FastMacro()
Application.ScreenUpdating = False ‘Disable screen updating to improve speed
For x = 2 To 50000
Cells(x, 1) = x
Cells(x, 2) = x + (x * 0.1)
Next x
Application.ScreenUpdating = True ‘Enable screen updating to show the final result
End Sub
After implementing these changes, you will find that the FastMacro runs significantly faster than the previous code. By using this approach, you can optimize macros to work faster and more efficiently when working on automation projects in the long run.