Forum Discussion
AdamWojcik
Jul 10, 2021Copper Contributor
Copy charts from all sheets to one
I have written a macro that looks like this: Sub CopyCharts()
Dim Sheet_Count As Integer
Dim Target_Sheet As Worksheet
Dim i As Integer
Sheet_Count = ActiveWorkbook...
- Jul 10, 2021
1) You have two For loops, but only one Next.
2) You declare a variable Target_Sheet.
3) You don't use the keyword Set to assign Target_Sheet
3) You try to paste to the undefined variable ChartSheet
4) You try to paste to a string instead of a range.
5) Chart is not a good name for a variable, since it is the name of a built-in object.
Here is a working version:
Sub CopyCharts() Dim Sheet_Count As Integer Dim Target_Sheet As Worksheet Dim i As Integer Dim Cht As ChartObject Sheet_Count = ActiveWorkbook.Sheets.Count Set Target_Sheet = ActiveWorkbook.Sheets(1) For i = 2 To Sheet_Count For Each Cht In ActiveWorkbook.Sheets(i).ChartObjects Cht.Copy Target_Sheet.Paste Target_Sheet.Range("D4") Next Cht Next i End Sub
6) All charts will be pasted more or less on top of each other
HansVogelaar
MVP
Replace the lines
For i = 2 To sheetCount
For Each Cht In ActiveWorkbook.Sheets(i).ChartObjects
Cht.Copy
targetSheet.Paste targetSheet.Range("A1")
Next Cht
Next i
with
Dim arr
arr = Array("A1", "A20", "J1", "J20")
For i = 1 To 4
Set Cht = Sheets(targetSheet.Index + i).ChartObjects(1)
Cht.Copy
targetSheet.Paste targetSheet.Range(arr(i - 1))
Next i
nbisceglie
Apr 20, 2022Copper Contributor
You are a life saver! I've been working on this for 2 days! Thank you so much!!
I didn't think of creating an array. I'm going to add this one to my tool belt haha.
I didn't think of creating an array. I'm going to add this one to my tool belt haha.