none
Plotting data in a userform RRS feed

  • Question

  • I need to plot data in a userform and I think I got it to work, but somehow 10 plots end up on the screen. I don't understand how that happens when in my code it clearly selects one range for the x values and another for the y values and that's it. Also, how would I plot 3 different lines on the same plot? For example, the x value data is located from D2:D17605 and the y value data is located from I2:I17605, J2:J17605, K2:K17605. I tried to add in the code for this (ChartData2,3, etc.), but I can't figure out how to correctly plot one line let alone 3.

    

    Thanks for any help. 

    This is my code so far:

    Private Sub CommandButton2_Click()
    
    Dim MyChart As Chart
    Dim ChartData As Range
    Dim chartIndex As Integer
    Dim ChartName As String
    
    chartIndex = ComboBox1.ListIndex
    
    Select Case chartIndex
    
    Case 0
    
    Set ChartData = Sheet1.Range("I2:I17605")
    Set ChartData2 = Sheet1.Range("J2:J17605")
    Set ChartData3 = Sheet1.Range("K2:K17605")
    ChartName = ("Crank Gear")
    
    Case 1
    
    Set ChartData = Sheet2.Range("I2:I17605")
    Set ChartData2 = Sheet2.Range("J2:J17605")
    Set ChartData3 = Sheet2.Range("K2:K17605")
    ChartName = ("Cam Gear")
    
    Case 2
    
    Set ChartData = Sheet3.Range("I2:I17605")
    Set ChartData2 = Sheet3.Range("J2:J17605")
    Set ChartData3 = Sheet3.Range("K2:K17605")
    ChartName = ("Fuel Pump Gear")
    
    End Select
    
    Application.ScreenUpdating = False
    
    Set MyChart = ActiveSheet.Shapes.AddChart(xlXYScatterLines).Chart
    
    MyChart.SeriesCollection.NewSeries
    MyChart.SeriesCollection(1).Name = ChartName
    MyChart.SeriesCollection(1).Values = ChartData
    MyChart.SeriesCollection(1).XValues = ActiveSheet.Range("D2:D17605")
    
    Dim imageName As String
    imageName = Application.DefaultFilePath & Application.PathSeparator & "Tempchart.gif"
    
    MyChart.Export Filename:=imageName
    
    ActiveSheet.ChartObjects(1).Delete
    
    Application.ScreenUpdating = True
    
    Inputs1.Image1.Picture = LoadPicture(imageName)
    
    End Sub

    • Edited by IbroT Tuesday, October 27, 2015 2:23 PM
    Tuesday, October 27, 2015 2:19 PM

All replies

  • Hi IbroT,

    >> but somehow 10 plots end up on the screen

    Please provide the screenshot and sample data. What’s the result if you create and change chart through UI? If it works fine, I suggest that you could record the macro, then check the code.

    To change x and y value data, your code is ok. If you have the issue, you could record the macro then try it again.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 28, 2015 6:37 AM
    Moderator
  • Hi, 

    Thanks for the reply.

    I can't add a chart because it says my account isn't verified, but it is so I am not sure what the issue is. I can email it to you if you would like to see it. 

    The x data is somehow getting multiplied by some factor of at least 1000, but it should only be from 0 to 99.9% (the final value for x taken from the graph is 500 million %). Maybe the issue is that I did not explicitly code that one percentage should align with one value for my other dataset (the y values)?

    Thanks.


    • Edited by IbroT Thursday, October 29, 2015 1:30 PM
    Thursday, October 29, 2015 1:17 PM
  • Hi IbroT,

    Could you reproduce that issue in a new file?

    You may share the same file or screenshot on the OneDrive, then post the link here. (If you can’t add links in the post, please select the links and click remove hyperlink icon to remove the links as regular text)

    https://support.office.com/en-us/article/Share-files-and-folders-and-change-permissions-9fcc2f7d-de0c-4cec-93b0-a82024800c07?ui=en-US&rs=en-US&ad=US

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 30, 2015 1:26 AM
    Moderator