Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Speed Up Excel VBA Macros

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Excel VBA macros can slow down significantly when they interact excessively with the worksheet, screen, or application events. Speeding them up reduces runtime from minutes to seconds, especially when processing large datasets. The key is minimizing interaction with Excel’s user interface and disabling automatic recalculations. No special tools are needed—just access to the VBA editor in Excel.

What You'll Need

  • Microsoft Excel (2010 or later)
  • A VBA macro that runs slowly
  • Access to the VBA Editor (press Alt+F11)

Step-by-Step

  1. Open the VBA Editor by pressing Alt+F11 while Excel is active.
  2. In the Project Explorer, locate the module containing your macro and double-click it to open the code window.
  3. At the very start of your macro procedure, add these three lines: Application.ScreenUpdating = False, Application.Calculation = xlCalculationManual, and Application.EnableEvents = False. These prevent Excel from refreshing the screen, recalculating formulas, and triggering event-driven code during execution.
  4. At the end of the macro, just before the End Sub line, add the corresponding restore lines: Application.ScreenUpdating = True, Application.Calculation = xlCalculationAutomatic, and Application.EnableEvents = True. This ensures Excel returns to normal behavior after the macro finishes.
  5. If your macro writes data to multiple cells, replace individual cell assignments with array-based operations. For example, instead of looping through 10,000 cells and writing each one, load data into a variant array first, then write the entire array to a range in one operation using Range("A1:Z1000") = myArray.
  6. Avoid using Select and Activate methods. Instead of Range("A1").Select followed by Selection.Value = "Hello", use Range("A1").Value = "Hello" directly.
  7. If your macro reads or writes from multiple worksheets, explicitly reference the worksheet object. Instead of Cells(i, j), use Worksheets("Sheet1").Cells(i, j) to avoid ambiguity and improve speed.
  8. For loops that process large ranges, declare variables as Long instead of Integer to handle larger row counts without overflow errors.
  9. Use With statements to reduce repeated object references. For example, wrap repeated references to a worksheet like With Worksheets("Data") and then use .Range("A1") inside the block.
  10. If your macro performs calculations, consider moving logic to a helper column in the worksheet and using Formula properties to set formulas in bulk, then converting them to values with Value = Value after calculation.

Common Mistakes

  • Forgetting to restore ScreenUpdating, Calculation, or EnableEvents to their original state. If the macro crashes or is interrupted, Excel may remain in a frozen state—this is why error handling is critical.
  • Using Range("A1").Select followed by ActiveCell or Selection unnecessarily. These methods add overhead and are prone to errors if the active sheet changes.
  • Writing to the worksheet inside a loop. Each write triggers a screen update and recalculation, even if those are disabled. Always batch writes.
  • Not declaring variable types. Using Dim i instead of Dim i As Long can cause performance penalties and runtime errors on large datasets.
  • Assuming that disabling events alone will speed up the macro. Events must be disabled along with screen updates and calculation to achieve maximum performance.

Tips

  • Use the Debug.Print statement with Timer to log execution time before and after key sections. For example: Dim t As Double: t = Timer at the start and Debug.Print "Section took: " & Timer - t & " seconds" after.
  • For read-heavy operations, load entire ranges into variant arrays using myArray = Range("A1:D1000").Value, process the array in memory, then write back once.
  • Use Application.CutCopyMode = False after copying ranges to clear the clipboard buffer, which can improve responsiveness.
  • If your macro runs after another macro or event, ensure EnableEvents = False is set before calling the next procedure to prevent cascading events.
  • Test performance improvements on a copy of your data. Large datasets can cause Excel to become unresponsive if not handled carefully.
  • Consider using Application.StatusBar to show progress during long operations, so users know the macro is still running.

Troubleshooting

  • If Excel freezes after running the macro, press Ctrl+Break to stop execution, then manually set Application.ScreenUpdating = True, Application.Calculation = xlCalculationAutomatic, and Application.EnableEvents = True in the Immediate Window (Ctrl+G in VBA Editor).
  • If formulas don’t recalculate after the macro ends, check that Application.Calculation = xlCalculationAutomatic was restored. If not, manually trigger recalculation with Calculate.
  • If events (like Worksheet_Change) are not firing after the macro, ensure Application.EnableEvents = True was executed. If it was skipped due to an error, you must reset it manually.
  • If performance didn’t improve, check for nested loops that still reference individual cells. Replace them with array operations or use Offset and Resize to work with ranges in bulk.
  • If the macro crashes with an overflow error, check that all loop counters are declared as Long, not Integer, especially if processing more than 32,767 rows.

Frequently Asked Questions

Why does my macro still run slowly even after disabling ScreenUpdating?

Disabling ScreenUpdating only stops screen refreshes. If calculation is automatic and events are enabled, Excel still recalculates formulas and triggers event code, which can dominate runtime. You must disable all three: ScreenUpdating, Calculation, and EnableEvents.

Can I speed up macros that use PivotTables?

Yes. Set PivotTable.ManualUpdate = True before making changes, then set it back to False after. This prevents Excel from rebuilding the PivotTable on every change.

Does using arrays really make a big difference?

Yes. Writing to a worksheet one cell at a time can take seconds per operation. Writing 10,000 values via an array takes under a second. Array operations bypass Excel’s cell-by-cell handling entirely.

Should I disable events for all macros?

Only if your macro modifies cells that trigger other macros (like Worksheet_Change). If your code doesn’t affect other event-driven logic, you can skip EnableEvents = False to avoid potential side effects.

What’s the fastest way to copy data between sheets?

Use Range("SourceRange").Copy Destination:=Range("TargetRange") instead of looping. Copy with a single operation is faster than reading into an array and writing back.

Why does Excel become unresponsive during long macros even with optimizations?

Large data operations still consume memory. If Excel runs out of available RAM, it may freeze. Break large tasks into chunks, save intermediate results, and use DoEvents sparingly to allow Windows to process UI events without slowing performance significantly.