Advertisement
gn4711

Excel save all charts

Aug 18th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'small nicety to ensure two-digits for better file sorting'
  2. Function NiceFileNumber(num As Integer) As String
  3.     If num < 10 Then
  4.         NiceFileNumber = "0" & num
  5.     Else
  6.         NiceFileNumber = num
  7.     End If
  8. End Function
  9.  
  10. 'the real function'
  11. Sub ExportAllCharts()
  12.     Dim i As Integer, exportCount As Integer
  13.     Dim fileNum As String, fileBase As String
  14.     Dim sheetObj As Worksheet
  15.     Dim chartObj As Chart
  16.  
  17.     'current file location and name, with extension stripped'
  18.    fileBase = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, ".") - 1)
  19.     exportCount = 0
  20.  
  21.     'First, export all charts that are in their own sheets'
  22.    For Each chartObj In ActiveWorkbook.Charts
  23.         fileNum = NiceFileNumber(exportCount)
  24.         exportCount = exportCount + 1
  25.  
  26.         'Do the export'
  27.        chartObj.Export fileBase & "_chart" & fileNum & ".png"
  28.     Next
  29.  
  30.     'Then, export all charts that are embedded inside normal sheets'
  31.    For Each sheetObj In ActiveWorkbook.Worksheets
  32.         For i = 1 To sheetObj.ChartObjects.Count
  33.             fileNum = NiceFileNumber(exportCount)
  34.             exportCount = exportCount + 1
  35.  
  36.             'Do the export'
  37.            sheetObj.ChartObjects(i).Activate
  38.             ActiveChart.Export fileBase & "_chart" & fileNum & ".png"
  39.         Next i
  40.     Next
  41.    
  42.     MsgBox "DONE"
  43. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement