Solved

How to update Excel 2010 charts ?

Posted on 2013-02-06
2
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

696 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