When an Excel VBA module runs slowly, it’s usually due to inefficient code interacting with the worksheet, not Excel itself. The fix involves optimizing how your code reads/writes cells, disables screen updating, and manages events. Below are proven steps to drastically reduce runtime.

What You’ll Need

  • Excel installed (any recent version: 2016, 2019, 2021, or Microsoft 365)
  • Basic familiarity with the Visual Basic Editor (VBE)
  • A working VBA module you want to speed up
  • No external add-ins or dependencies required

Step-by-Step

  1. Open the Visual Basic Editor (VBE) by pressing Alt+F11 while in Excel.
  2. In the VBE, locate your slow module in the Project Explorer (press Ctrl+R if it’s hidden), then double-click the module to open its code window.
  3. At the top of your main procedure (the Sub or Function that runs first), add these two lines immediately after the Sub declaration:
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    These prevent Excel from redrawing the screen or triggering other macros during execution, saving significant time.
  4. Before the End Sub line of your procedure, add:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    This ensures Excel returns to normal behavior after your code finishes—even if an error occurs (see Step 7 for error handling).
  5. Replace cell-by-cell reads/writes with array-based operations. For example, instead of looping through cells like this:
    For i = 1 To 1000
        Cells(i, 1).Value = Cells(i, 2).Value * 2
    Next i
    Use this approach:
    Dim dataRange As Range
    Dim dataArray As Variant
    Set dataRange = Range("B1:B1000")
    dataArray = dataRange.Value
    For i = 1 To UBound(dataArray, 1)
        dataArray(i, 1) = dataArray(i, 1) * 2
    Next i
    dataRange.Offset(0, -1).Value = dataArray
    Reading a range into an array once (via dataArray = dataRange.Value) and writing back in one operation is far faster than 2,000+ individual cell interactions.
  6. Avoid using Select, Activate, or ActiveCell. Instead of:
    Range("A1").Select
    Selection.Value = "Done"
    Write directly:
    Range("A1").Value = "Done"
    Selecting objects forces Excel to update the UI unnecessarily, slowing execution.
  7. Wrap your procedure in error handling to guarantee ScreenUpdating and EnableEvents reset even if your code crashes. Use this structure:
    Sub MyMacro()
        On Error GoTo CleanExit
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        
        ' Your main code here...
        
    CleanExit:
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        If Err.Number <> 0 Then Err.Raise Err.Number, Err.Source, Err.Description
    End Sub
    This prevents Excel from staying in a frozen state after a crash.
  8. If your macro writes to many cells, consider using Application.Calculate = xlCalculationManual at the start and Application.Calculate = xlCalculationAutomatic at the end (with error handling), especially if your workbook has formulas. This stops Excel from recalculating after every write.

Common Mistakes

  • Forgetting to re-enable ScreenUpdating or EnableEvents after the macro finishes—this leaves Excel sluggish or unresponsive.
  • Using Range("A1:A1000").Select followed by Selection.Interior.Color = ... instead of applying properties directly to the range object.
  • Looping through cells individually instead of loading data into arrays first, especially on large datasets (>500 rows).
  • Calling other macros or user-defined functions (UDFs) inside tight loops without realizing they trigger recalculations or additional overhead.
  • Placing worksheet functions like WorksheetFunction.CountIf inside loops when a single array-based approach would suffice.

Tips

  • For repeated lookups, load data into a Scripting.Dictionary object once, then query it in memory—much faster than VLOOKUP in a loop.
  • Turn off automatic calculation only if your sheet doesn’t rely on real-time formula updates during the macro (e.g., simple data imports).
  • Use Debug.Print Timer before and after critical sections to measure performance. For example:
    Debug.Print "Start: " & Timer
    ' ... code ...
    Debug.Print "End: " & Timer
    This reveals which part of your code is the bottleneck.
  • When deleting rows, filter data first and delete visible rows in one go instead of looping and deleting row by row.
  • Avoid using Copy and PasteSpecial in code; direct value assignment (e.g., destRange.Value = srcRange.Value) is more efficient and avoids the clipboard.
  • Declare variables with specific types (e.g., Dim i As Long instead of Dim i) to reduce type conversion overhead.

Troubleshooting

If your macro is still slow after applying the above:

  • Check for external dependencies: If your code connects to databases, web APIs, or other applications (e.g., Word, Outlook), those network or COM calls dominate runtime. Move them outside loops or batch requests.
  • Verify no other macros are running: Open the VBE, go to Debug > Compile VBA Project. If errors appear, fix them—uncompiled or broken code can cause unexpected delays.
  • Test with a minimal workbook: Copy your module into a blank workbook with just sample data. If it runs fast there, your original file may have heavy conditional formatting, volatile formulas (e.g., INDIRECT, OFFSET), or add-ins interfering.
  • Disable add-ins temporarily: Start Excel in safe mode (Windows+R, type excel /safe, press Enter), then run your macro. If it speeds up, one add-in is causing the slowdown.
  • Check for infinite loops: Add a counter or DoEvents inside loops to allow system updates. If Excel freezes completely (no cursor movement, no progress), your loop logic may be stuck.
  • Review large range usage: If you reference entire columns (e.g., Range("A:A")), narrow to the actual used range: Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).

Frequently Asked Questions

Why does my VBA macro run fast in a new workbook but slow in my main file?

Your main workbook likely contains complex formulas (especially volatile ones like INDIRECT), conditional formatting rules, or add-ins that trigger recalculations during macro execution. Disabling automatic calculation and events (as shown above) often resolves this.

Is there a maximum size for arrays in VBA?

Yes, but it’s rarely a practical limit. A 1D array can hold up to ~2 billion elements, and a 2D array up to ~16 million rows × 65,536 columns—far beyond typical Excel rows (1,048,576). Memory constraints (RAM) matter more than VBA’s internal limits.

Should I disable calculation for all macros?

Only if your macro modifies data that doesn’t rely on formulas updating instantly. If your macro reads formula results (e.g., Cells(i, 3).Value = Cells(i, 1).Value + Cells(i, 2).Value), disabling calculation could cause incorrect outputs. Use it selectively.

Can using .Value2 instead of .Value improve speed?

Yes, slightly. .Value2 returns raw data without currency/date formatting overhead, avoiding variant type conversions. For large numeric datasets, it’s marginally faster—but the biggest gains come from array operations, not this detail.

Why does my macro still show a spinning cursor even after disabling ScreenUpdating?

ScreenUpdating = False only hides visual changes. If your code performs long-running tasks (e.g., file I/O, network calls), Excel remains busy. Use DoEvents sparingly inside long loops to yield control, but only if you’re certain your code won’t cause re-entrancy issues.

Does using With blocks make VBA faster?

Not significantly for performance, but it reduces repeated object resolution. For example, With Range("A1").Font is cleaner and slightly more efficient than repeating Range("A1").Font.Bold = True multiple times. Prioritize array-based I/O and disabling events first.