Solved

How to update Excel 2010 charts ?

Posted on 2013-02-06
2
361 Views
Last Modified: 2013-02-27
Dear All,
I created excel file called "Dashborad" with only one sheet "Day 1"
This sheet contains alot of charts with data coming from another excel file called "Data" and from the sheet "D1"

What i need to do is to copy the Day1 sheet "with the sharts" to other sheets within the same file but each sheet should read from different  sheest in "Data" file.

To make is simple
 "Dashborad"- day 1 should read from "Data"-D1
 "Dashborad"- day 2 should read from "Data"-D2

and so on

is there any way to update the links automatically instead of passing on each chart and adjust the data source manually,
0
Comment
Question by:mostabdo
  • 2
2 Comments
 
LVL 26

Accepted Solution

by:
redmondb earned 500 total points
ID: 38860084
mostabdo,

The attached is a proof of concept. There are still a couple of issues with it...
(1) It only checks the Charts' Series. So, if a Title used a link then this will not be updated. If you want me to change this then please let me know which parts of your charts include links to the D1 sheet.
(2) It assumes that each Day's data has the same number of rows (and columns). Before I change this, please confirm that the sheets do have differing numbers of rows. (I'm lazy so I don't want to do this work unless you actually need it!)

The code is
Option Explicit

Sub Copy_Charts()
Dim xNew     As Worksheet
Dim xSheet   As Worksheet
Dim xData    As Workbook
Dim xCharts  As Workbook
Dim xChart   As ChartObject
Dim xSeries  As Series
Dim xDays    As Long
Dim xSkipped As Long


If Not Book_Exists("Data.xlsm") Then
    MsgBox ("""Data.xlsm"" must be open before starting this run - job cancelled.")
    Exit Sub
End If

Set xData = Workbooks("Data.xlsm")
Set xCharts = ThisWorkbook

If Not Sheet_Exists("Day 1", xCharts.Name) Then
    MsgBox ("""Day 1"" sheet not found in """ & xCharts.Name & """  - job cancelled.")
    Exit Sub
End If

Application.ScreenUpdating = False
    
    For Each xSheet In xData.Sheets
        xDays = xDays + 1
        If Mid(xSheet.Name, 1, 1) <> "D" Or Len(xSheet.Name) = 1 Then
            xSkipped = xSkipped + 1
            MsgBox ("Processing of Sheet """ & xSheet.Name & """ skipped as its name is invalid.")
        Else
            If Sheet_Exists("Day " & Mid(xSheet.Name, 2, 9999), xCharts.Name) Then
                xSkipped = xSkipped + 1
                If xSheet.Name <> "D1" Then MsgBox ("Processing of """ & "Day " & Mid(xSheet.Name, 2, 9999) & """ skipped as it already exists.")
            Else
                xCharts.Sheets("Day 1").Copy before:=xCharts.Sheets("Day 1")
                Set xNew = ActiveSheet
                xNew.Name = "Day " & Mid(xSheet.Name, 2, 9999)
                For Each xChart In xNew.ChartObjects
                    For Each xSeries In xChart.Chart.SeriesCollection
                        xSeries.Formula = Replace(xSeries.Formula, "]D1!", "]D" & Mid(xSheet.Name, 2, 9999) & "!")
                    Next
                Next
            End If
        End If
    Next
    
    xCharts.Sheets("Day 1").Move before:=xCharts.Sheets(1)

Application.ScreenUpdating = True

MsgBox (xDays & " sheets in """ & xData.Name & """ processed, of which " & xSkipped & IIf(xSkipped = 1, " was", " were") & " skipped.")

End Sub

Function Sheet_Exists(xSheet_Name As String, Optional xBook As String) As Boolean

If xBook = "" Then xBook = ActiveWorkbook.Name

Sheet_Exists = False

On Error Resume Next
    Sheet_Exists = (Workbooks(xBook).Sheets(xSheet_Name).Name = xSheet_Name)
On Error Resume Next

End Function

Function Book_Exists(xBook_Name As String) As Boolean

Book_Exists = False

On Error Resume Next
    Book_Exists = (Workbooks(xBook_Name).Name = xBook_Name)
On Error GoTo 0

End Function

Open in new window

Regards,
Brian.Dashboard.xlsmData.xlsm
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38933508
Thanks, mostabdo.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question