Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub CommandButton1_Click()
- Dim ch As Chart
- Dim objChartObject As ChartObject
- Dim ws As Worksheet
- Dim strExportPath As String
- Dim strFileName As String
- Dim lngWSChartsCount As Long
- Dim lngChartSheetsCount As Long
- strExportPath = ThisWorkbook.Path 'Change if you want different path
- 'Export charts from worksheets
- lngWSChartsCount = 0
- For Each ws In Worksheets
- For Each objChartObject In ws.ChartObjects
- Set ch = objChartObject.Chart
- On Error Resume Next
- strFileName = ch.ChartTitle.Text & ".jpg"
- ch.Export Filename:=strExportPath & "" & strFileName, FilterName:="JPG"
- If Err <> 0 Then
- Err.Clear
- strFileName = objChartObject.Name & ".jpg"
- ch.Export Filename:=strExportPath & "" & strFileName, FilterName:="JPG"
- 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
- 'Export also chart sheets
- lngChartSheetsCount = 0
- For Each ch In ThisWorkbook.Charts
- On Error Resume Next
- strFileName = ch.ChartTitle.Text & ".jpg"
- ch.Export Filename:=strExportPath & "" & strFileName, FilterName:="JPG"
- If Err <> 0 Then
- Err.Clear
- strFileName = ch.Name & ".jpg"
- ch.Export Filename:=strExportPath & "" & strFileName, FilterName:="JPG"
- If Err = 0 Then
- lngChartSheetsCount = lngChartSheetsCount + 1
- End If
- Else
- lngChartSheetsCount = lngChartSheetsCount + 1
- End If
- On Error GoTo 0
- strFileName = vbNullString
- Next ch
- Call MsgBox(lngWSChartsCount & " charts from worksheets and " & lngChartSheetsCount & " chart sheets exported at " & strExportPath, vbInformation, "Charts Export Result")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement