Copy multiple charts to multiple worksheets
Solved/Closed
ibtrini
Posts
7
Registration date
Saturday August 17, 2013
Status
Member
Last seen
September 5, 2013
-
Aug 30, 2013 at 01:05 PM
ibtrini Posts 7 Registration date Saturday August 17, 2013 Status Member Last seen September 5, 2013 - Sep 5, 2013 at 03:23 PM
ibtrini Posts 7 Registration date Saturday August 17, 2013 Status Member Last seen September 5, 2013 - Sep 5, 2013 at 03:23 PM
Related:
- Copy multiple charts to multiple worksheets
- How to delete multiple files on mac - Guide
- Allow multiple downloads chrome - Guide
- How to make multiple selections in photoshop - Guide
- Mpc-hc multiple instances - Guide
- How to share multiple contacts on android - Guide
4 responses
If I may,
Your scope is quite a challenge! Is this a school project?
To address the question:
"I would like to copy all 197 charts at the same time rather than doing them one at a time - Is this possible? "
--No. it will happen serially!
Your scope is quite a challenge! Is this a school project?
To address the question:
"I would like to copy all 197 charts at the same time rather than doing them one at a time - Is this possible? "
--No. it will happen serially!
ibtrini
Posts
7
Registration date
Saturday August 17, 2013
Status
Member
Last seen
September 5, 2013
Aug 30, 2013 at 08:16 PM
Aug 30, 2013 at 08:16 PM
No, it is not a school project. VBA, Macros, and Excel is not my strength however being in IT I was asked for some input prior (create a workbook with worksheets for each country based on a template and another a data workbook). They were so pleased with the end result they decided to ask if it was possible to included charts (from another workbook) which will enhance their prior project. I do not mind helping anyone as I always end up gaining more knowledge in the long run.
I did not understand what you meant by "No, it will happen serially." Is it possible to have this automated so it can run thru a loop to accomplish this step or can it only be done manually one at a time?
Thanks for replying.
I did not understand what you meant by "No, it will happen serially." Is it possible to have this automated so it can run thru a loop to accomplish this step or can it only be done manually one at a time?
Thanks for replying.
ibtrini
Posts
7
Registration date
Saturday August 17, 2013
Status
Member
Last seen
September 5, 2013
Aug 31, 2013 at 09:13 PM
Aug 31, 2013 at 09:13 PM
Thanks for taking the time to think about this.
"Country" workbook :
Worksheets (197) named for each country - Angola, Algeria... Zimbabwe. Each country is on its own worksheet.
Each worksheet has relevant data (17 rows) pertaining to that country (Population, drinking patterns - % males, % females, consumption by type - beer, wines, spirits... etc)
"Charts" workbook has 1 worksheet:
Column A - Country names (Angola, Algeria... Zimbabwe) on 197 rows with charts. Chart 1 refers to Angola, Chart 2 refers to Algeria... Chart 197 refers to Zimbabwe.
Desired outcome:
A macro to update the "Country" worksheets to include respective charts from the "Charts" workbook. In other words, the "Angola" worksheet should contain the "Angola" chart (Chart 1) on row 18, "Algeria" worksheet should contain the "Algeria" chart (Chart 2) on row 18 etc...
Thanks again.
"Country" workbook :
Worksheets (197) named for each country - Angola, Algeria... Zimbabwe. Each country is on its own worksheet.
Each worksheet has relevant data (17 rows) pertaining to that country (Population, drinking patterns - % males, % females, consumption by type - beer, wines, spirits... etc)
"Charts" workbook has 1 worksheet:
Column A - Country names (Angola, Algeria... Zimbabwe) on 197 rows with charts. Chart 1 refers to Angola, Chart 2 refers to Algeria... Chart 197 refers to Zimbabwe.
Desired outcome:
A macro to update the "Country" worksheets to include respective charts from the "Charts" workbook. In other words, the "Angola" worksheet should contain the "Angola" chart (Chart 1) on row 18, "Algeria" worksheet should contain the "Algeria" chart (Chart 2) on row 18 etc...
Thanks again.
ibtrini
Posts
7
Registration date
Saturday August 17, 2013
Status
Member
Last seen
September 5, 2013
Sep 5, 2013 at 03:23 PM
Sep 5, 2013 at 03:23 PM
Well I finally figured out a solution to my problem so I will post it here in case it can be useful to anyone else. This exercise was part of a prior routine (https://ccm.net/forum/affich-716133-macro-to-create-tabs-and-populate-from-other-worksheets#p716553) so I will give a recap and post the solution below.
1st workbook (SampleFile) contained 2 worksheets (Data and Template)
"Data" - Column A - 197 country names, Columns B-BU - 72 different pieces of information on each country
"Template" - Column A - Field names referenced from above. Column B to receive populated data with a chart below it.
2nd workbook (ChartFile) contained 1 worksheet (AFR A)
Charts for each country - 197 charts
Desired result was to create a macro to add worksheets for each country, rename the worksheet to the appropriate country name, populate that worksheet with a template then populate it with the relevant data from "SampleFile" and the respective chart from "ChartFile". If the macro needs to be rerun the country tabs will need to be deleted first.
The only way I could get the chart to paste accurately into the "SampleFile" was by using "ActiveSheet.Pictures.Paste.Select". What was just as confusing was the pasted object was not a picture and it even had the server location of the file/series being used. I am sure there are some redundant lines of coding in here but for now it does what I needed it to do.
Sub CreateSheetsFromAList()
' Create an array
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("Data").Range("A2")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
' Loop through country range - Get Country name
For Each MyCell In MyRange
' Worksheet creation - Add new, blank worksheet
Sheets.Add After:=Sheets(Sheets.Count)
' Worksheet renaming - Rename to appropriate country
Sheets(Sheets.Count).Name = MyCell.Value
' Select Range from "Template" to use with newly created worksheet
Sheets("Template").Select
Range("A1:M32").Select
Selection.Copy
Sheets(Sheets.Count).Select
Range("A1").Select
' The copy sequence here made a huge difference
' Copy layout from the source template (formatting cells)
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
' Copy static data from the source template
ActiveSheet.Paste
' Assign Country value to cell to be used in the "Match" function
Range("B1:B1") = MyCell.Value
' Populate newly created worksheet using string concatenation in the "Index" function
Range("B3:B3") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A3,Data!B1:J1,0))"
Range("B4:B4") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A4,Data!B1:J1,0))"
Range("B8:B8") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A8,Data!B1:J1,0))"
Range("B9:B9") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A9,Data!B1:J1,0))"
Range("B12:B12") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A12,Data!B1:J1,0))"
Range("B13:B13") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A13,Data!B1:J1,0))"
Range("B14:B14") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A14,Data!B1:J1,0))"
Range("B16:B16") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A16,Data!B1:J1,0))"
' Copy charts from the "Charts" workbook and paste as a picture in the "SampleFile" 'workbook under the appropriate country worksheet.
Sheets(Sheets.Count).Select
ActiveSheet.ChartObjects("Chart 1").Activate
Windows("ChartsFile.xlsm").Activate
' Select appropriate worksheet in "ChartsFile"
Sheets("AFR A").Select
ActiveSheet.ChartObjects(MyCell.Value).Activate
ActiveChart.ChartArea.Copy
Windows("SampleFile.xlsm").Activate
ActiveSheet.Pictures.Paste.Select
' Return to beginning of loop
Next MyCell
Application.CutCopyMode = False
End Sub
1st workbook (SampleFile) contained 2 worksheets (Data and Template)
"Data" - Column A - 197 country names, Columns B-BU - 72 different pieces of information on each country
"Template" - Column A - Field names referenced from above. Column B to receive populated data with a chart below it.
2nd workbook (ChartFile) contained 1 worksheet (AFR A)
Charts for each country - 197 charts
Desired result was to create a macro to add worksheets for each country, rename the worksheet to the appropriate country name, populate that worksheet with a template then populate it with the relevant data from "SampleFile" and the respective chart from "ChartFile". If the macro needs to be rerun the country tabs will need to be deleted first.
The only way I could get the chart to paste accurately into the "SampleFile" was by using "ActiveSheet.Pictures.Paste.Select". What was just as confusing was the pasted object was not a picture and it even had the server location of the file/series being used. I am sure there are some redundant lines of coding in here but for now it does what I needed it to do.
Sub CreateSheetsFromAList()
' Create an array
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("Data").Range("A2")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
' Loop through country range - Get Country name
For Each MyCell In MyRange
' Worksheet creation - Add new, blank worksheet
Sheets.Add After:=Sheets(Sheets.Count)
' Worksheet renaming - Rename to appropriate country
Sheets(Sheets.Count).Name = MyCell.Value
' Select Range from "Template" to use with newly created worksheet
Sheets("Template").Select
Range("A1:M32").Select
Selection.Copy
Sheets(Sheets.Count).Select
Range("A1").Select
' The copy sequence here made a huge difference
' Copy layout from the source template (formatting cells)
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
' Copy static data from the source template
ActiveSheet.Paste
' Assign Country value to cell to be used in the "Match" function
Range("B1:B1") = MyCell.Value
' Populate newly created worksheet using string concatenation in the "Index" function
Range("B3:B3") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A3,Data!B1:J1,0))"
Range("B4:B4") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A4,Data!B1:J1,0))"
Range("B8:B8") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A8,Data!B1:J1,0))"
Range("B9:B9") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A9,Data!B1:J1,0))"
Range("B12:B12") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A12,Data!B1:J1,0))"
Range("B13:B13") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A13,Data!B1:J1,0))"
Range("B14:B14") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A14,Data!B1:J1,0))"
Range("B16:B16") = "=INDEX(Data!B2:J5,MATCH(" & MyCell.Value & "!B1,Data!A2:A5,0),MATCH(" & MyCell.Value & "!A16,Data!B1:J1,0))"
' Copy charts from the "Charts" workbook and paste as a picture in the "SampleFile" 'workbook under the appropriate country worksheet.
Sheets(Sheets.Count).Select
ActiveSheet.ChartObjects("Chart 1").Activate
Windows("ChartsFile.xlsm").Activate
' Select appropriate worksheet in "ChartsFile"
Sheets("AFR A").Select
ActiveSheet.ChartObjects(MyCell.Value).Activate
ActiveChart.ChartArea.Copy
Windows("SampleFile.xlsm").Activate
ActiveSheet.Pictures.Paste.Select
' Return to beginning of loop
Next MyCell
Application.CutCopyMode = False
End Sub