Extract Chart Data
by Jon Peltier
Peltier Technical Services, Inc., Copyright © 2009.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
This post presents a VBA procedure that extracts all data from a chart, and places it into a new worksheet. Why would you want to extract a chart’s data? Sometimes a chart gets its data from diverse sources, and you’d like to have the data in one place. You may receive a workbook that contains a chart, but the chart’s data is in a workbook which you don’t have access to. For whatever reason, this procedure comes in handy.
Sub ExtractChartData()
Dim iSrs As Long
Dim cht As Chart
Dim srs As Series
Dim ws As Worksheet
If ActiveChart Is Nothing Then Exit Sub
Set cht = ActiveChart
Set ws = Worksheets.Add
For iSrs = 1 To cht.SeriesCollection.Count
Set srs = cht.SeriesCollection(iSrs)
On Error Resume Next
ws.Cells(1, 2 * iSrs).Value = srs.Name
ws.Cells(2, 2 * iSrs - 1).Resize(srs.Points.Count).Value = _
WorksheetFunction.Transpose(srs.XValues)
ws.Cells(2, 2 * iSrs).Resize(srs.Points.Count).Value = _
WorksheetFunction.Transpose(srs.Values)
Next
End Sub
The procedure merely extracts the data, it does not change the data source of the chart to the ranges in the new worksheet. If desired, you could embellish this macro, so it restores information such as chart type, series formatting (marker or fill style and colors, line or border styles and colors), axis types and scales, axis and chart titles, data labels, and more.
If you are not sure how to use this procedure, read How To Use Someone Else’s Macro.
Possibly Related Posts:
- VBA to Split Data Range into Multiple Chart Series
- VBA Conditional Formatting of Charts by Series Name
- Dynamic Chart using Pivot Table and VBA
- Label Each Series in a Chart
- Stack Columns In Order Of Size With VBA
- VBA to Filter Chart Data Range
- Update Regular Chart when Pivot Table Updates
- Make Your Recorded Macro Independent of Which Sheet is Active
- Show Two Groups of Data in One Chart
- VBA Conditional Formatting of Charts by Value
Posted: Tuesday, August 26th, 2008 under VBA.
Comments: 8
Comments
Comment from Clint
Time: Tuesday, August 26, 2008, 2:28 pm
Wow! I think this little utility is something that might come in very handy – thanks!
Comment from Haffy
Time: Thursday, August 28, 2008, 5:04 am
Thanks, Jon. I have to extract data from charts regularly and sorted out a workbook to do it – but your code is so much faster – one of those “Why on earth didn’t I try that?” moments.
Thanks again.
Comment from TV
Time: Thursday, September 4, 2008, 5:51 pm
Is there a series property or some other trick to get the address of the chart data (or at least a point)?
Comment from Jon Peltier
Time: Thursday, September 4, 2008, 6:23 pm
TV -
This can be done by parsing the series formula. John Walkenbach has code to do this:
Comment from John
Time: Friday, March 13, 2009, 5:14 pm
I want something where I canextract only some of the data points from a chart.
I have a series of data with a date and price which I plot in excel. I want to be able to click or double click on the graph on a subset of the displayed points and have the dates for the selected points transferred to another part of the spreadsheet so I can do some cycle analysis on this set of points.
Does anyone know how to do this.
Thanks
John
Comment from Jon Peltier
Time: Friday, March 13, 2009, 10:52 pm
You can write some code that will respond to mouse clicks in the chart:
Chart Events in Microsoft Excel
Get Information about a Point in an Embedded Chart (zip file)
I’ve used similar techniques to help users select a range of points for regression or other detailed analysis, to highlight certain points, or to sort points into two subsets. It’s a lot of work to set up, but it’s slick from the user’s perspective.
Comment from John
Time: Saturday, March 14, 2009, 2:33 am
Thanks Jon,
I had just found your post “Get INformation about a point in an embedded chart” today and it does what I need, if I could also somehow highlite the selected points so the user knows which ones he has selected tha would also be beficial.
John
Comment from Jon Peltier
Time: Saturday, March 14, 2009, 9:00 am
Just time for a quick response to give you an idea. The click returns the series number (Arg1) and the point number (Arg2). You could have your code act on
ActiveChart.SeriesCollection(Arg1).Points(Arg2)
If the marker is a blue diamond, change it to a red square; otherwise change it back.
Good idea for a blog post. Unfortunately I have a backlog if ideas and a shortage of time.
















Write a comment
I welcome comments from my readers. If you have an opinion on this post, if you have a question or if there is anything to add, I want to hear from you. Whether you agree or disagree, please join the discussion.
If you want to include an image in your comment, post it on your own site or on one of the many free image sharing sites, and include a link in your comment. I'll download your image and insert the necessary html to display the image inline.
Read the PTS Blog Comment Policy.