Solved

How to update Excel 2010 charts ?

Posted on 2013-02-06
2
359 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
Comment Utility
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
Comment Utility
Thanks, mostabdo.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article will show you how to use shortcut menus in the Access run-time environment.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now