Solved

How to update Excel 2010 charts ?

Posted on 2013-02-06
2
362 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

792 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