Jul 10 2021 02:58 AM
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.Sheets.Count
Target_Sheet = ActiveWorkbook.Sheets(1)
For i = 2 To Sheet_Count
For Each Chart In ActiveWorkbook.Sheets(i).ChartObjects
Chart.Copy
ChartSheet.Paste ("D4")
Next i
End Sub
Its purpose is to copy all the charts from sheet 2 onwards to D4 on sheet 1.
It does so through these steps:
1. Get the sheet count of the active workbook
2. Set the copy target sheet to the first sheet
3. Loop through each but the first sheet
4. Inside that loop, copy all the chart objects to space D4 on the first sheet
When I try to run it, I get an error saying
Compile error:
Invalid Next control variable reference
The only place I use the Next instruction in is at the end of the For loop, which is how you're supposed to use it?
I'm new to both VBA and Office macros, so please forgive me if I'm making an obvious mistake
Oct 27 2022 12:54 AM
That code expects that the workbook has 5 sheets, and that you want to place pictures on the 2nd to 5th sheet, at the specified cells.
The code will cause an error if the workbook has more than 5 sheets.
Could you explain what you want to do?
Oct 27 2022 02:53 PM
Oct 27 2022 03:07 PM
If you want help with this, please provide detailed and specific information.