(Solved) – How to plot graph for every alternate column in VBA


How do i plot graph when the data that i want to plot lies on every alternate column in VBA(not manually select the data)? I tried to use the below code but it gives me two empty graph which im not sure which part of my codes am i missing out or done wrong. If done correctly, it should be graphs something like the one shown in “expected output”.

Current output
enter image description here

Expected output

enter image description here

Sub plotgraph()

 Dim i As Long, c As Long
    Dim shp As Shape
    Dim Cht As Chart
    Dim rngDB As Range, rngX As Range
    Dim Srs As Series
    Dim ws As Worksheet

     Set ws = Sheets("Data")

    Set rngDB = ws.UsedRange

    c = rngDB.Columns.Count
 Set shp = ws.Shapes.AddChart
Set Cht = shp.Chart

    With Cht
        For i = 1 To c Step 2 'For every column so in step2
            With ws
                Set rngX = ws.Range(.Cells(1, i), .Cells(1, i).End(xlDown))

            End With
        Next i

    ActiveChart.ChartType = xlXYScatter
'    ActiveChart.SetSourceData Source:=Range("Data!$A:$A")
    ActiveChart.Axes(xlValue).MinimumScale = 6.45
    ActiveChart.Axes(xlValue).MinimumScale = 5
    ActiveChart.Axes(xlValue).MaximumScale = 6.8
    ActiveChart.Axes(xlValue).MaximumScale = 9
End With

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *