aabrahamson
New Member
- Joined
- Jun 15, 2014
- Messages
- 13
Hi All,
I have a great VBA that exports all charts from a workbook and I wanted to change it to do all charts from a worksheet. I thought I could just take out the first loop (For & Next) no such luck see below working code that I wish to modify.
I have a great VBA that exports all charts from a workbook and I wanted to change it to do all charts from a worksheet. I thought I could just take out the first loop (For & Next) no such luck see below working code that I wish to modify.
Code:
Sub ExportAllChartsjpg()
Dim ch As Chart
Dim objChartObject As ChartObject
Dim ws As Worksheet
Dim strExportPath As String
Dim strFileName As String
Dim lngWSChartsCount As Long
strExportPath = ThisWorkbook.Path 'Change if you want different path
'Export charts from worksheets
lngWSChartsCount = 0
For Each ws In Worksheets
ws.Activate
For Each objChartObject In ws.ChartObjects
Set ch = objChartObject.Chart
On Error Resume Next
strFileName = ch.ChartTitle.Text & ".pdf"
ch.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strExportPath & "\" & strFileName
If Err <> 0 Then
Err.Clear
strFileName = ch.ChartTitle.Text & ".pdf"
ch.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strExportPath & "\" & strFileName
If Err = 0 Then
lngWSChartsCount = lngWSChartsCount + 1
End If
Else
lngWSChartsCount = lngWSChartsCount + 1
End If
On Error GoTo 0
strFileName = vbNullString
Next objChartObject
Next ws
Call MsgBox(lngWSChartsCount & " charts from worksheets exported to " & strExportPath, vbInformation, "Charts Export Result")
End Sub
Last edited: