VBA & Macro Problems

How to Fix Excel 2021 Crashing from VBA Chart Macros

Kushani NimanthikaKushani Nimanthika Nov 2, 2026 869 views

Question details

The user is experiencing unexpected closures in Excel 2021 when executing a VBA macro designed to format an active chart, despite the same macro working successfully in Office 2019.

How to Fix Excel 2021 Crashing Caused by VBA Chart Macros
Product
Microsoft Excel 2021
Device & OS
not provided
Scenario
Running a VBA script to format, edit, or adjust plot elements on an active chart.
Observed behavior
Excel 2021 crashes and closes unexpectedly during the execution of the chart-formatting macro.
Before you start

Before modifying your VBA script, ensure you have saved a backup copy of your Excel workbook (.xlsm) to prevent any accidental loss of your original code.

Solution 1Recommended

Optimize VBA Code by Removing Active Object References

Refactor your VBA macro to use direct object references instead of relying on the Select, Selection, and ActiveCell methods, which are slower and prone to causing crashes in newer Excel versions.

Excel 2021 handles memory and active-object references differently than Excel 2019. Code that relies heavily on selecting objects or manipulating an active chart can cause memory conflicts, leading to unexpected application closures.

By replacing active selections with direct variable references and updating your loop counters, you can significantly improve the stability and execution speed of your VBA macros.

1
Remove Select and Selection methods

Open the VBA Editor and search your script for '.Select', 'Selection', and 'ActiveCell'. Rewrite these lines to apply formatting directly to the objects without selecting them first.

2
Declare proper Chart variables

Assign your target chart to a specific variable using 'Dim myChart As Chart', and then set it directly via 'Set myChart = ActiveSheet.ChartObjects(1).Chart' instead of relying on an ActiveChart.

3
Update series loops dynamically

Instead of assuming a fixed number of series (e.g., 30), use a dynamic count by looping through 'SeriesCollection.Count'. This prevents the macro from attempting to format non-existent series.

4
Use Long data types for loop counters

Change the data type of your loop variables from 'Integer' to 'Long' (e.g., 'Dim i As Long'). In modern Office architectures, Long is processed more efficiently and prevents overflow errors.

5
Safely clear chart formatting

Use your new chart variable to directly target and clear plot formatting, remove gridlines, titles, and borders rather than clearing formats through the Selection object.

Optimize VBA Code by Removing Active Object References
Direct Referencing Advantage: Directly referencing Chart and Series objects not only stops Excel from crashing but also speeds up macro execution significantly because the screen does not need to constantly redraw selections.
Free Microsoft Office alternative

Experience Stable VBA Macros with WPS Office

If Microsoft Excel 2021 continues to crash during complex macro executions, consider switching to WPS Office. It offers a highly compatible, lightweight, and free alternative that supports advanced spreadsheets and VBA macros without the instability found in some older Office updates.

  1. 1. Download and install: Get the free version of WPS Office from the official website and install it on your device.
  2. 2. Open your macro workbook: Launch WPS Spreadsheet and open your existing .xlsm file containing the VBA macros.
  3. 3. Run macros stably: Enable macros when prompted and run your chart-formatting scripts directly within the highly stable WPS VBA environment.
Fully compatible with Microsoft Excel (.xlsx, .xlsm) formats and legacy VBA scripts.Built-in support for VBA macros and advanced spreadsheet functions out of the box.Lightweight architecture that efficiently handles heavy script execution and large datasets.Familiar user interface allows for a seamless, zero-learning-curve migration.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my macro work perfectly in Excel 2019 but crash in Excel 2021?

Excel 2021 introduced subtle changes to its calculation engine and how it manages screen rendering and memory. Macros that heavily rely on selecting objects or manipulating the ActiveChart can cause memory conflicts in newer versions, whereas direct object references bypass these rendering updates and execute safely.

What is the difference between Integer and Long in Excel VBA?

In modern 32-bit and 64-bit environments, VBA processes 'Long' variables faster than 'Integer'. Additionally, 'Integer' has a much lower maximum value limit (32,767). If a loop or count exceeds this limit, it will cause an overflow error and potentially crash Excel. 'Long' avoids this issue safely.

How do I check if a chart is active before running my macro code?

You can use an IF statement in VBA to verify the TypeName of the current selection before proceeding. Using the syntax `If TypeName(Selection) = "ChartArea" Then` ensures the macro only executes its chart-formatting logic when a chart is properly selected.

Is it possible to format an Excel chart without selecting it first?

Yes. By assigning the chart object to a variable (for example, `Set myChart = ActiveSheet.ChartObjects("Chart 1").Chart`), you can change its formatting, titles, plot areas, and gridlines directly through that variable without ever using the `.Select` or `.Activate` commands.