Excel

Dynamic Charting Range

Ease of Use

Easy

Version tested with

2000, 2002 

Submitted by:

Justinlabenne

Description:

Uses a Worksheet_Change event to automatically adjust an embedded chart objects data range. 

Discussion:

Use as a template for quick charting. No need to set a data range, the code will update for new series and data as long as your data table is set up correctly. 

Code:

instructions for use

			

Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) ' Credit to Aaron Blood as the originator of this code: ' http://www.xl-logic.com/pages/charts.html (DynamicChart3.zip) Dim oChrt As ChartObject Dim szSeries As String On Error GoTo ErrExit Application.EnableEvents = False Set oChrt = ActiveSheet.ChartObjects(1) If Not oChrt Is Nothing Then For Each oChrt In ChartObjects szSeries = oChrt.Chart.SeriesCollection(1).Formula szSeries = Left(szSeries, InStrRev(szSeries, ",") - 1) szSeries = Right(szSeries, (Len(szSeries) - InStrRev(szSeries, ","))) oChrt.Chart.SetSourceData Source:=Range(szSeries).CurrentRegion Next oChrt End If Set oChrt = Nothing ErrExit: Application.EnableEvents = True End Sub

How to use:

  1. Open an Excel Workbook
  2. Copy the code
  3. Right Click on a Sheet tab > View Code
  4. Paste code into the right pane
  5. Press Alt+Q to return to Excel
  6. Save workbook before any other changes
 

Test the code:

  1. Add a chart to the worksheet
  2. Create a data table
  3. Chart will update accordingly
 

Sample File:

DynamicCharting.zip 11.42KB 

Approved by mdmackillop


This entry has been viewed 314 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express