VBA – Save a Chart as a Picture (Gif)

With the current article I will simple present some easy code, transferring Chart (diagram) to a picture. It is not something unusual, but sometimes it may be very handy, if you are trying to use the picture later.

MS_Excel

So, let’s imagine that you want to store the first diagram of your Excel spreadsheet to the location of the Excel file. VBA comes with an easy solution for this. It is the following:

Sub SaveChartAsPicture()

    Dim chChart             As Chart
    Dim sFileLocation       As String
    Dim sFileName           As String
    
    On Error Resume Next
    
    sFileName = InputBox("Please, insert file name:", "File Name")
    
    Set chChart = ActiveSheet.ChartObjects(1).Chart
    
    sFileLocation = ThisWorkbook.Path & "\" & sFileName & ".gif"
    chChart.Export Filename:=sFileLocation, FilterName:="GIF"
    
    On Error GoTo 0

End Sub

Pretty much, the code can be easily broken, if you set an empty space in the InputBox or a character, forbidden in naming usage. Anyway, that is not the case 🙂 Enjoy it if you can 🙂