Plotting with VBA

Gitad

New Member
Joined
Jun 13, 2024
Messages
5
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hi, I am a beginner and trying to do a simple plot with VBA. The issue is x values are y values are plotted separately; however it works fine (the second plot) when I do manually. The Demo Plot is the one with VBA code as below and incorrect.
Any help would be greatly appreciated.

'Embed the plot in the same worksheet
Sub Charts_Example2()

Dim Ws As Worksheet
Dim rng As Range
Dim MyChart As Object

Set Ws = Worksheets("Sheet1") 'Sheet reference
Set rng = Ws.Range("A1:B5") 'Data reference
Set MyChart = Ws.Shapes.AddChart2 'Sets the chart object

With MyChart.Chart
.SetSourceData rng
.ChartType = xlXYScatterLines
.ChartTitle.Text = "Demo Plot"
End With

End Sub




View attachment 112626
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi, I am a beginner and trying to do a simple plot with VBA. The issue is x values are y values are plotted separately; however it works fine (the second plot) when I do manually. The Demo Plot is the one with VBA code as below and incorrect.
Any help would be greatly appreciated.

'Embed the plot in the same worksheet
Sub Charts_Example2()

Dim Ws As Worksheet
Dim rng As Range
Dim MyChart As Object

Set Ws = Worksheets("Sheet1") 'Sheet reference
Set rng = Ws.Range("A1:B5") 'Data reference
Set MyChart = Ws.Shapes.AddChart2 'Sets the chart object

With MyChart.Chart
.SetSourceData rng
.ChartType = xlXYScatterLines
.ChartTitle.Text = "Demo Plot"
End With

End Sub




View attachment 112626
 

Attachments

  • excel_VBA_plot.jpg
    excel_VBA_plot.jpg
    92.3 KB · Views: 7
Upvote 0
Try it
VBA Code:
Sub Charts_Example2_1()
    Dim Ws As Worksheet
    Dim rng As Range
    Dim rngXVal As Range
    Dim MyChart As Object

    Set Ws = Worksheets("Sheet1")
    Set rng = Ws.Range("B1:B5")
    Set rngXVal = Ws.Range("A2:A5")

    Set MyChart = Ws.Shapes.AddChart2(227, xlLine).Chart

    With MyChart
        .SetSourceData Source:=rng
        .FullSeriesCollection(1).XValues = rngXVal
    End With
End Sub

Artik
 
Upvote 0
Solution
Try it
VBA Code:
Sub Charts_Example2_1()
    Dim Ws As Worksheet
    Dim rng As Range
    Dim rngXVal As Range
    Dim MyChart As Object

    Set Ws = Worksheets("Sheet1")
    Set rng = Ws.Range("B1:B5")
    Set rngXVal = Ws.Range("A2:A5")

    Set MyChart = Ws.Shapes.AddChart2(227, xlLine).Chart

    With MyChart
        .SetSourceData Source:=rng
        .FullSeriesCollection(1).XValues = rngXVal
    End With
End Sub

Artik
Thanks a lot, Artik! That works fine.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top