none
Displaying Chart in Userform RRS feed

  • Question

  • Hey!

    Does anybody knows how can I display a Dynamic Chart (Type:= XYScatterSmoothNoMarkers) on Userform!

    I have created chart on sheet2 which represent different variables. How to Display it in UserForm that User could have option to change variables and chart would dynamically represent them....

    My code of Chart:

    Sub ChartCreate()
       
        Dim sh As Worksheet
        Dim ChartPu As Chart
       
            Set sh = ActiveWorkbook.Worksheets("Sheet2")
            Set ChartPu = sh.Shapes.AddChart.Chart
           
            With ChartPu
                .Parent.Name = "MyChart"
                .ChartType = xlXYScatterSmoothNoMarkers
                .Parent.Height = Application.CentimetersToPoints(8)
                .Parent.Width = Application.CentimetersToPoints(20)
            End With
           
    End Sub

    Friday, December 19, 2014 8:21 AM

Answers

  • That can not be the actual code you are using as it is incomplete.

    Once corrected it worked for me.

    Userform with Image and commandbutton

    Option Explicit
    
    Dim ChartPu As Chart
    
    
    Private Sub CommandButton1_Click()
        UpdateChart
    End Sub
    
    
    Private Sub UpdateChart()
        Dim Fname As String
        Set ChartPu = Sheets("Sheet2").ChartObjects("MyChart").Chart
        With ChartPu
            .Parent.Width = 300
            .Parent.Height = 150
        End With
        'Save chart as GIF
        Fname = ThisWorkbook.Path & Application.PathSeparator & "temp.gif"
        ChartPu.Export Filename:=Fname, FilterName:="GIF"
       
        'Show the chart
        Image1.Picture = LoadPicture(Fname)
           
    End Sub
    


    Cheers,

    Andy
    www.andypope.info

    • Marked as answer by GyTasS Friday, December 19, 2014 10:18 AM
    Friday, December 19, 2014 9:59 AM

All replies

  • You can not display a 'live' chart, instead you can show an image of it.

    http://spreadsheetpage.com/index.php/tip/displaying_a_chart_in_a_userform/


    Cheers,

    Andy
    www.andypope.info

    Friday, December 19, 2014 9:33 AM
  • Ok, I figure out, that cannot display 'live' chart, but seems that I cannot  display even image of chart... This is My code:

    Maybe I missed something?

    Option Explicit

    Dim ChartPu As Chart

    Private Sub UpdateChart()
        Set ChartPu = Sheets("Sheet2").ChartObjects("MyChart").Chart
        With ChartPu
            .Parent.Width = 300
            .Parent.Height = 150
           
        'Save chart as GIF
        Fname = ThisWorkbook.Path & Application.PathSeparator & "temp.gif"
        ChartPu.Export Filename:=Fname, FilterName:="GIF"
       
        'Show the chart
        Image1.Picture = LoadPicture(Fname)
           
    End Sub

    Friday, December 19, 2014 9:53 AM
  • That can not be the actual code you are using as it is incomplete.

    Once corrected it worked for me.

    Userform with Image and commandbutton

    Option Explicit
    
    Dim ChartPu As Chart
    
    
    Private Sub CommandButton1_Click()
        UpdateChart
    End Sub
    
    
    Private Sub UpdateChart()
        Dim Fname As String
        Set ChartPu = Sheets("Sheet2").ChartObjects("MyChart").Chart
        With ChartPu
            .Parent.Width = 300
            .Parent.Height = 150
        End With
        'Save chart as GIF
        Fname = ThisWorkbook.Path & Application.PathSeparator & "temp.gif"
        ChartPu.Export Filename:=Fname, FilterName:="GIF"
       
        'Show the chart
        Image1.Picture = LoadPicture(Fname)
           
    End Sub
    


    Cheers,

    Andy
    www.andypope.info

    • Marked as answer by GyTasS Friday, December 19, 2014 10:18 AM
    Friday, December 19, 2014 9:59 AM