How to Plot VBA Arrays Directly in Excel Charts
Question details
The user wants to create Excel chart series using VBA arrays directly, bypassing the need to output array data into worksheet cells.

- Product
- Excel, WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Creating dynamic charts using VBA macros where data is stored in memory arrays rather than in worksheet ranges.
- Observed behavior
- The goal is to successfully assign VBA arrays directly to the XValues and Values properties of a chart series.
Ensure that your VBA arrays are properly populated and that the arrays intended for the X and Y axes have matching lengths and compatible bounds.
Assign Arrays Directly to Chart Series Properties
Use VBA to create a chart, clear default series, and assign your in-memory arrays directly to the chart's XValues and Values properties.
By directly passing arrays to the chart series object, you can keep your spreadsheet clean and improve the performance of your macros by avoiding unnecessary read/write operations to the worksheet.
Use the Charts.Add or Shapes.AddChart method in your VBA script to instantiate a new chart in your active worksheet.
Loop through the chart's SeriesCollection and delete any existing auto-generated series to ensure you are starting with a blank canvas.
Use the SeriesCollection.NewSeries method to create a data series for your first set of array values.
Set your category axis array by assigning it to the XValues property (e.g., s.XValues = UX) and set your value axis array by assigning it to the Values property (e.g., s.Values = UY).
Repeat the NewSeries process to add a second series. Assign another array (e.g., s2.Values = yp) while using the same XValues array to plot multiple lines on the same chart.

Create and Manage VBA Charts with WPS Spreadsheet
WPS Spreadsheet offers robust VBA and Macro support, allowing you to easily run advanced scripts to plot memory arrays directly into dynamic charts.
- 1. Access the VBA Editor: Open WPS Spreadsheet and navigate to the Developer tab to launch the VBA Editor.
- 2. Write your array macro: Insert a new Module and write your macro to populate your UX and UY arrays with data.
- 3. Assign arrays to the chart: Use standard VBA charting methods to assign your populated arrays to the SeriesCollection.Values and XValues properties.
- 4. Run the script: Execute the macro to instantly generate your chart directly from memory, without modifying any cell data.

Frequently Asked Questions
Why am I getting a 'Type Mismatch' error when assigning arrays to a chart?
This usually happens if the array is not formatted properly or if it is multidimensional. Ensure your arrays are 1D arrays before assigning them to the XValues or Values properties.
Is there a limit to how many data points I can plot directly from a VBA array?
Yes, charting engines have a limitation on the string length of the SERIES formula. If your array contains too many data points, the formula length may exceed the limit (usually around 8192 characters), causing a runtime error.
Can I plot a 2D array directly to a chart series?
No, the XValues and Values properties expect a 1D array. If you have a 2D array, you must first extract the desired column or row into a separate 1D array before assigning it to the chart.
How do I format the chart lines and markers using VBA?
After assigning the arrays to your series, you can modify the Format.Line and MarkerStyle properties of the Series object directly within your VBA script to customize colors and shapes.




