Displaying “live” chart data in Excel Userform

前端 未结 1 548
独厮守ぢ
独厮守ぢ 2021-02-11 10:20

I have been researching the modes of displaying charts in a userform.

The general consensus seems to be to save the chart as a .GIF file and then upload this within the

相关标签:
1条回答
  • 2021-02-11 10:50

    Case 1: If the chart is on the worksheet, it will be easier as below:

    Private Sub UserForm_Initialize()
        Dim Fname As String
    
        Call SaveChart
        Fname = ThisWorkbook.Path & "\temp1.gif"
        Me.Image1.Picture = LoadPicture(Fname)
    End Sub
    
    Private Sub SaveChart()
        Dim MyChart As Chart
        Dim Fname As String
    
        Set MyChart = Sheets("Data").ChartObjects(1).Chart
        Fname = ThisWorkbook.Path & "\temp1.gif"
        MyChart.Export Filename:=Fname, FilterName:="GIF"
    End Sub
    

    Case 2: If chart is not on the worksheet, you may need to create a temporary chart, save it as GIF, delete it, and finally load the picture {

    Me.Image1.Picture = LoadPicture(Fname)
    

    } when the Userform is initialized.

    Case 1 is easier to code. The above code still works even I cut and paste the chart in a later-hidden worksheet.

    0 讨论(0)
提交回复
热议问题