Solved

excel vba copying chart objects to a new workbook

Posted on 2011-03-09
6
936 Views
Last Modified: 2012-08-13
hi

I have a source workbook , call it WorkbookA, with multiple sheets that have chartobjects. I am copying over all the sheets to a WorkbookB. However, on doing so, I find that the charts in the copied over chartobjects in WorkbookB refer to data set in  WorkbookA.

How can I change this so that the charts in WorkbookB refer to data sets in WorkbookB?

I tried to change the Formula so that it refers to WorkBookB, but on doing so it gives a Run-Time error '1004' : Application-defined or object-defined error.

After copying over all the sheets from WorkbookA.xlsm to WorkbookB.xls, do the following


Set NewBook = Application.Workbooks.Open("WorkbookB.xls")
For Each sSheet In NewBook.Sheets
        With sSheet
                For Each chtTemp In .ChartObjects
                    For Each objSeries In chtTemp.Chart.SeriesCollection
                        objSeries.Formula = Replace(objSeries.Formula, "WorkbookA.xlsm", "WorkbookB.xls")
                    Next
                Next
        End With
    Next sSheet
   
    NewBook.Save
    NewBook.Close
0
Comment
Question by:LuckyLucks
[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
  • 3
  • 2
6 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35086312
Here is a trick to it without writing too much code.

Rename the sheets which has a chart to say "Lucky1","Lucky2" etc...

Then in the code,

1) Add a new workbook.
2) Move (don't copy) the chart sheet and the source datasheet to the new workbook for example

Sheets("Lucky1").Move After:=Workbooks("NewWorkbook").Sheets(3)

3) Similarly move all sheets from the 1st one to the new book. Ensure that you handle errors. And then close the 1st workbook without saving so the saved version still contains the moved, i.e., removed, sheets

This will ensure that the new workbook's chart refer to the actual sheets.

Let me know if you get stuck.

Sid
0
 

Author Comment

by:LuckyLucks
ID: 35086734
moving is not acceptable. I need to keep the original for a reason. also note that the original WorkBookA is a macro-enabled file while the second WorkbookB is not a macro-enabled file.

Do you know what the error means? I don't need a alternate solution that is totally operating on a different workflow.
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35086904
>>>moving is not acceptable. I need to keep the original for a reason.

Your original file will remain untouched and that is why at the end of the code we will not save the original. If you upload your files, then maybe I can give you a sample code?

Sid
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 6

Expert Comment

by:Faisal Khan
ID: 35087344
Try to copy your data sheets first and then chart object sheets. doing so, copied chart would not refer to previous workbook
0
 

Accepted Solution

by:
LuckyLucks earned 0 total points
ID: 35088911
found a similar question by brettdj that was great:

brettdj:

If you are copying the data range as well then you can just relink the destination workbook to itself (from the source workbook) using ChangeLink in VBA

This gets the Chart pointing to the data in the destination folder

Cheers

Dave
0
 

Author Closing Comment

by:LuckyLucks
ID: 35126442
original answer provided by brettdj
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

Suggested Solutions

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 article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

738 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