How to copy charts from multiple sheets and save it as chart title

EileenJohn

Board Regular
Joined
Nov 23, 2016
Messages
53
Hi, I'm trying to copy charts from workbookA (sheets1, sheets3 and Sheets4) to .png and save the charts based on chart title
(TotalStaff, Factory5, LeaveMonthly).
But now I only manage to get chart from 1 sheets.
My code:
Sub CopyChart()
Dim pic_rng As Range
Dim ShTemp As Worksheet
Dim ChTemp As Chart
Dim PicTemp As Picture
Application.ScreenUpdating = False
Set pic_rng = Worksheets("Factory5").Range("A1:Q21")
Set ShTemp = Worksheets.Add
DoEvents
Charts.Add
Application.DisplayAlerts = False
ActiveChart.Location Where:=xlLocationAsObject, Name:=ShTemp.Name
Set ChTemp = ActiveChart
pic_rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
DoEvents
ChTemp.Paste
Set PicTemp = Selection
With ChTemp.Parent
.Width = PicTemp.Width + 540
.Height = PicTemp.Height + 310
End With
ChTemp.Export Filename:="D:\Charts\Factory5.png", FilterName:="png"
ShTemp.Delete
Application.ScreenUpdating = True
End Sub

How to change this code? Should i use select case? For? How to save it according to chart title? Any ideas? Thanks in advance.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi

Code:
Sub CopyChart()
Dim pic_rng As Range, ShTemp As Worksheet, ChTemp As Chart, _
PicTemp As Picture, tit, sn, i%
sn = Array("sheet1", "sheet3", "sheet4")
tit = Array("TotalStaff", "Factory5", "LeaveMonthly")
Set ShTemp = Worksheets.Add
For i = LBound(sn) To UBound(sn)
    Set pic_rng = Worksheets(sn(i)).[A1:Q21]                    ' variable sheet
    DoEvents
    Charts.Add
    Application.DisplayAlerts = False
    ActiveChart.Location xlLocationAsObject, ShTemp.Name
    Set ChTemp = ActiveChart
    ChTemp.HasTitle = True
    ChTemp.ChartTitle.Text = tit(i)
    pic_rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    DoEvents:    ChTemp.Paste
    Set PicTemp = Selection
    With ChTemp.Parent
        .Width = PicTemp.Width + 540
        .Height = PicTemp.Height + 310
    End With
    ChTemp.Export "c:\pub\" & tit(i) & ".png", "png"            ' variable file name
    DoEvents: ShTemp.[a1].Activate
    ShTemp.ChartObjects(1).Delete
Next
ShTemp.Delete
End Sub
 
Upvote 0
Hi

Code:
Sub CopyChart()
Dim pic_rng As Range, ShTemp As Worksheet, ChTemp As Chart, _
PicTemp As Picture, tit, sn, i%
sn = Array("sheet1", "sheet3", "sheet4")
tit = Array("TotalStaff", "Factory5", "LeaveMonthly")
Set ShTemp = Worksheets.Add
For i = LBound(sn) To UBound(sn)
    Set pic_rng = Worksheets(sn(i)).[A1:Q21]                    ' variable sheet
    DoEvents
    Charts.Add
    Application.DisplayAlerts = False
    ActiveChart.Location xlLocationAsObject, ShTemp.Name
    Set ChTemp = ActiveChart
    ChTemp.HasTitle = True
    ChTemp.ChartTitle.Text = tit(i)
    pic_rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    DoEvents:    ChTemp.Paste
    Set PicTemp = Selection
    With ChTemp.Parent
        .Width = PicTemp.Width + 540
        .Height = PicTemp.Height + 310
    End With
    ChTemp.Export "c:\pub\" & tit(i) & ".png", "png"            ' variable file name
    DoEvents: ShTemp.[a1].Activate
    ShTemp.ChartObjects(1).Delete
Next
ShTemp.Delete
End Sub
Thanks worf
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,612
Members
449,109
Latest member
Sebas8956

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