VBA copy charts appearance to .png

EileenJohn

Board Regular
Joined
Nov 23, 2016
Messages
53
Hi, anyone know how to copy charts appearance and save it as chart title? For example in workbook A , I have sheets1, chart title Factory5. And I want to save it as Factory5.png.
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
The code above only set for 1 sheets. What if I want to apply it to workbook? For example: In WorkbookA, sheets1 and sheets4.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
This code will export all the charts in your workbook:


Code:
Public Sub ExportCharts()
  Dim chtobj As ChartObject
  Dim sht As Object
  
  For Each sht In ThisWorkbook.Sheets
    If TypeOf sht Is Chart Then
      Call ExportChart(sht)
    ElseIf TypeOf sht Is Worksheet Then
      For Each chtobj In sht.ChartObjects
        Call ExportChart(chtobj.Chart)
      Next chtobj
    End If
  Next sht
  
  Shell "C:\Windows\explorer.exe " & ThisWorkbook.Path, vbNormalFocus
End Sub


Private Sub ExportChart(ByVal cht As Chart)
  cht.Export ThisWorkbook.Path & "\" & cht.ChartTitle.Text & ".png"
End Sub
 
Upvote 0
This code will export all the charts in your workbook:


Code:
Public Sub ExportCharts()
  Dim chtobj As ChartObject
  Dim sht As Object
  
  For Each sht In ThisWorkbook.Sheets
    If TypeOf sht Is Chart Then
      Call ExportChart(sht)
    ElseIf TypeOf sht Is Worksheet Then
      For Each chtobj In sht.ChartObjects
        Call ExportChart(chtobj.Chart)
      Next chtobj
    End If
  Next sht
  
  Shell "C:\Windows\explorer.exe " & ThisWorkbook.Path, vbNormalFocus
End Sub


Private Sub ExportChart(ByVal cht As Chart)
  cht.Export ThisWorkbook.Path & "\" & cht.ChartTitle.Text & ".png"
End Sub

Thanks gpeacock. But I want to resize my charts, where should I put this code?
DoEvents
ChTemp.Paste
Set PicTemp = Selection
With ChTemp.Parent
.Width = PicTemp.Width + 540
.Height = PicTemp.Height + 310
End With
ChTemp.Export
 
Upvote 0
If you want to resize the charts as well, use the following code instead:
Code:
Public Sub ExportCharts()
  Dim chtobj As Excel.ChartObject
  Dim intCharts As Integer
  Dim sht As Object
  
  For Each sht In ThisWorkbook.Sheets
    If TypeOf sht Is Chart Then
      Call ExportChart(sht, True)
      intCharts = intCharts + 1
    ElseIf TypeOf sht Is Worksheet Then
      For Each chtobj In sht.ChartObjects
        Call ExportChart(chtobj.Chart)
        intCharts = intCharts + 1
      Next chtobj
    End If
  Next sht
  
  If intCharts > 0 Then
    Shell "C:\Windows\explorer.exe " & ThisWorkbook.Path, vbNormalFocus
  Else
    MsgBox "No charts were found.", vbExclamation, "Export Charts"
  End If
End Sub

Private Sub ExportChart(cht As Chart, Optional ByVal blnChartSheet As Boolean = False)
  If Not blnChartSheet Then
    cht.Parent.Width = 384  ' Set dimensions
    cht.Parent.Height = 180 ' as required
  End If
  
  cht.Export ThisWorkbook.Path & "\" & cht.ChartTitle.Text & ".png"
End Sub
 
Upvote 0
If you want to resize the charts as well, use the following code instead:
Code:
Public Sub ExportCharts()
  Dim chtobj As Excel.ChartObject
  Dim intCharts As Integer
  Dim sht As Object
  
  For Each sht In ThisWorkbook.Sheets
    If TypeOf sht Is Chart Then
      Call ExportChart(sht, True)
      intCharts = intCharts + 1
    ElseIf TypeOf sht Is Worksheet Then
      For Each chtobj In sht.ChartObjects
        Call ExportChart(chtobj.Chart)
        intCharts = intCharts + 1
      Next chtobj
    End If
  Next sht
  
  If intCharts > 0 Then
    Shell "C:\Windows\explorer.exe " & ThisWorkbook.Path, vbNormalFocus
  Else
    MsgBox "No charts were found.", vbExclamation, "Export Charts"
  End If
End Sub

Private Sub ExportChart(cht As Chart, Optional ByVal blnChartSheet As Boolean = False)
  If Not blnChartSheet Then
    cht.Parent.Width = 384  ' Set dimensions
    cht.Parent.Height = 180 ' as required
  End If
  
  cht.Export ThisWorkbook.Path & "\" & cht.ChartTitle.Text & ".png"
End Sub
Thanks gpeacock
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,611
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