Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jacksonpradolima/9c428e9e5df9f8ce43aee8bcce040e64 to your computer and use it in GitHub Desktop.
Save jacksonpradolima/9c428e9e5df9f8ce43aee8bcce040e64 to your computer and use it in GitHub Desktop.
Export a chart from Excel to PDF or PNG

Intro

These code are to export a chart from Excel to .png and .pdf extensions.

How to proceed

go to Tools > Macro > Visual Basic Editor.

  1. In the new window, select Insert > Module and copy some from codes available (SaveSelectedChartAsPDF or SaveSelectedChartAsImage) or in the blank page.
  2. Then go to File > Close > Return to Microsoft Excel
  3. Select a chart that you want to export
  4. Tools > Macro > Macros, then select SaveSelectedChartAsPDF or SaveSelectedChartAsImage and press Execute.
  5. Check the image at the folder (current directory).

To export chart as PDF

Sub SaveSelectedChartAsPDF()
    With ActiveChart.PageSetup
        .Orientation = xlLandscape
        .FitToPagesTall = 1
        .FitToPagesWide = 1
        .LeftMargin = 0
        .RightMargin = 0
        .BottomMargin = 0
        .TopMargin = 0
    End With
    ActiveChart.ExportAsFixedFormat xlTypePDF, ActiveWorkbook.Path + "\figure.pdf", xlQualityStandard, False, False
End Sub

To export chart as PNG

Sub SaveSelectedChartAsImage()
    ActiveChart.Export "figure.png"
End Sub```
Sub SaveSelectedChartAsImage()
ActiveChart.Export "figure.png"
End Sub
Sub SaveSelectedChartAsPDF()
With ActiveChart.PageSetup
.Orientation = xlLandscape
.FitToPagesTall = 1
.FitToPagesWide = 1
.LeftMargin = 0
.RightMargin = 0
.BottomMargin = 0
.TopMargin = 0
End With
ActiveChart.ExportAsFixedFormat xlTypePDF, ActiveWorkbook.Path + "\figure.pdf", xlQualityStandard, False, False
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment