Copy charts from one sheet to another sheet with the same name as the chart itself

Johan W

New Member
Joined
Nov 22, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi
I have a problem that i hope someone can help me with.

I got a excelfile containing 50 sheets , named "15HP", "03AE" etc. , and one sheet that contains charts,
The sheets names is the same as the names of the charts.

So, what i want to do is to get the charts with the same name as my worksheets, copyed to the worksheet
Instead of coding with hard values, like my code below, i want to make my macro more dynamic.
With dynamic, i mean that the macro should loop thru each sheet and copy the right chart.



Regards from Sweden

Johan

Sub test()
'

Sheets("charts").Select
ActiveSheet.ChartObjects("15HP").Activate
Selection.Copy
Sheets("15HP").Select
Range("H10:K33").Select
ActiveSheet.Paste
ActiveSheet.Pictures.Paste.Select
Range("A1").Select


Sheets("charts").Select
ActiveSheet.ChartObjects("03AE").Activate
Selection.Copy
Sheets("03AE").Select
Range("H10:K33").Select
ActiveSheet.Paste
ActiveSheet.Pictures.Paste.Select
Range("A1").Select

and so on....
.....


End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
The following macro will loop through each chartobject within the worksheet called "charts", and copy/paste each chart into their respective worksheets. Note that it assumes that all of the relevant worksheets are contained in the workbook running the code. If you want the macro to run based on the active workbook instead, replace...

VBA Code:
Set sourceWorkbook = ThisWorkbook

with

VBA Code:
Set sourceWorkbook = ActiveWorkbook

Here's the macro...

VBA Code:
Sub test()

    Dim sourceWorkbook As Workbook
    Set sourceWorkbook = ThisWorkbook

    Dim sourceWorksheet As Worksheet
    Set sourceWorksheet = sourceWorkbook.Worksheets("charts")
    
    Dim currentChartObject As ChartObject
    For Each currentChartObject In sourceWorksheet.ChartObjects
        currentChartObject.Copy
        With sourceWorkbook.Worksheets(currentChartObject.Name)
            .Paste .Range("H10")
        End With
    Next currentChartObject

End Sub

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top