How to show a PivotChart in UserForm

  • Hi experts,


    I am trying to show a PivotChart in a userform using the code below:



    However, I am getting the below error msg:


    Run-time error - The item with the specified name wan't found


    I believe I am using the incorrect object (chart) but not sure how to add a pivot chart in the line code.


    Appreciate any help.






    Edited 2 times, last by KjBox ().

  • You have not assigned a value to the ChartName variable.


    Either use the actual name of the chart you want to use, for example

    Code
    Set CurrentChart = ThisWorkbook.Sheets("Charts").ChartObjects("Chart1").Chart

    Or, if there is just the 1 chart on the Charts sheet you could use

    Code
    Set CurrentChart = ThisWorkbook.Sheets("Charts").ChartObjects(1).Chart

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Hi KjBox,


    Thanks for replying, actually I am using two option buttons to populate the ChartName parameter. Though not sure why the code work only for the first chart (Chart2wks)) and when I select the other chart (Chart3wks) I keep getting the error:

    Runtime Error 481; Invalid Picture



    Set
    CurrentChart = ThisWorkbook.Sheets("Charts").ChartObjects(ChartName).Chart


    2 wks coverage option button

    Private Sub Opt2WksCov_Click()

    Call ChangeChart("Chart2wks")

    End Sub


    3 wks coverage option button

    Private Sub Opt3WksCov_Click()

    Call ChangeChart("Chart3wks")

    End Sub



    Sub ChangeChart (ChartName As String)


    Dim CurrentChart As Chart

    Dim FName As String


    FName = ThisWorkbook.Path & "\temp.gif"


    Set CurrentChart = ThisWorkbook.Sheets("Charts").ChartObjects(ChartName).Chart


    CurrentChart.Export Filename:=FName, filtername:="GIF"


    FormCharts.ImgChart.Picture = LoadPicture(FName)



    End Sub

  • If you have already created a .gif image named "temp.gif" then you could be getting the error because you are attempting to create a second .gif image with the same name.


    Try killing the temp.gif file before exporting a new image

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • You're welcome

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!