Chart & Visualization Issues

How to Create an Excel Chart from a Selected Range Using VBA

John WilsonJohn Wilson Nov 3, 2026 869 views

Question details

The user wants to modify an existing VBA macro to generate a chart based exclusively on the currently highlighted cells instead of charting a fixed range or an entire region.

How to Create an Excel Chart from a Selected Range Using VBA
Product
Excel
Device & OS
not provided
Scenario
Automating dynamic chart creation using VBA where the data scope depends on the user's real-time worksheet selection.
Observed behavior
Existing macros use properties like EntireRow or CurrentRegion, which plot all contiguous data and include unwanted cells rather than isolating the specific user-selected range.
Before you start

Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that the Developer tab is enabled in your ribbon to access the VBA Editor.

Solution 1Recommended

Use the Selection Object as the Chart Source

Change the data source reference in your VBA code from CurrentRegion or EntireRow to Selection to plot exactly what the user highlights.

When VBA macros use CurrentRegion or UsedRange, they automatically grab all contiguous data around the active cell. Using the Selection property guarantees the chart relies solely on the exact cells you highlighted before running the macro.

1
Open the VBA Editor

Press Alt + F11 to launch the VBA Editor, then click Insert > Module to create a new module.

2
Write the Charting Macro

Create a new Sub procedure (e.g., Sub CreateChartFromSelection()) and declare a Chart variable using 'Dim C As Chart'.

3
Add Chart Shape and Assign Selection

Set the chart variable to a new chart shape, then assign the source data using the code: C.SetSourceData Source:=Selection.

4
Run the Macro on Selected Data

Return to your worksheet, highlight the specific row or cells you want to chart, and run the macro to generate the targeted visualization.

Use the Selection Object as the Chart Source
Pro Tip: You can assign this macro to a custom button on your worksheet or a keyboard shortcut for faster, single-click chart generation.
Advanced Automation

Create Dynamic Charts Instantly in WPS Spreadsheet

WPS Spreadsheet offers robust VBA and Macro support, allowing you to run your existing Excel macros seamlessly. You can easily automate chart generation from selected ranges without changing your workflow.

  1. 1. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your existing .xlsm file containing your chart data.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon and click on 'VBA Editor' to manage your macros.
  3. 3. Insert the Selection Macro: Paste your customized VBA code utilizing the 'Selection' object to ensure only highlighted cells are charted.
  4. 4. Highlight and Run: Select the exact data range in your spreadsheet, then run the macro to instantly generate your tailored chart.
Fully compatible with Microsoft Excel file formats (.xlsx, .xlsm)Comprehensive VBA and Macro support for advanced data automationLightweight application with high performance when processing large datasetsFree to use with an intuitive, user-friendly interface
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA chart include blank rows or extra data?

If your macro uses properties like 'CurrentRegion' or 'EntireRow', it selects all contiguous cells or the whole row respectively. Changing the source to 'Selection' restricts the chart data to just the cells you have manually highlighted.

Can I use this macro on non-contiguous selected ranges?

Yes, VBA's 'Selection' object can handle multiple non-adjacent ranges (e.g., holding the Ctrl key while selecting cells), but ensure that your chosen chart type supports plotting non-contiguous data properly.

How do I assign my chart creation macro to a custom button?

Go to the Developer tab, insert a Form Control Button onto your worksheet, right-click the new button, select 'Assign Macro', and choose your dynamic chart creation macro from the list.