Solved

How to update Excel 2010 charts ?

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

690 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