1

I have a problem with my graphs. After transmitting the values for the x- and y- axis, I have the problem that sometimes the origin moves, therefore, I need to reset the origin to its normal position, nevertheless, I do not know how to address the origin of a graph in VBA and to manipulate it?

Thanks in Advance

Community
  • 1
  • 1
Claude G
  • 23
  • 6

1 Answers1

0

When inserting a new chart:

Set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 200).Chart

When adjusting an old chart:

'Set myChart = Sheets(1).
With myChart
     .Top = 50
     .Left = 50
End With

To Name the Series:

.SeriesCollection(1).Name = Range("B" & lngStartRow - 1).Value
.SeriesCollection(2).Name = Range("C" & lngStartRow - 1).Value

To change the series data itself:

set CHARTDATA = range("A1:A2").values
myChart.Chart.SetSourceData Source:=CHARTDATA

To change the x-axis on which the dataseries are placed, try this, dependant on the values in A1 and A2:

With myChart.Chart.Axes(xlCategory)
    .MinimumScale = ActiveSheet.Range("A1").Value
    .MaximumScale = ActiveSheet.Range("A2").Value
End With

Note that I haven't had time to check the last series data, let me know if it works and i can update later if not.

Was this what you were looking for?

Preston
  • 7,399
  • 8
  • 54
  • 84
  • @Claude G, please respond here, your response below isn't an answer, so should be in the comments with the relevant question. Cheers – Preston Nov 30 '16 at 11:14
  • In parts yes, but if I do it this way, the whole chart is moved and for example the lines are no more adjusted to the initial size, so I want to adjust the origins of my lines in the graph so that everything fits again with the other graphs. – Claude G Nov 30 '16 at 12:23
  • I'm sorry I don't understand, you want to change the data series or the axis? Can you post the code you have so I can update that? – Preston Nov 30 '16 at 13:01
  • Let say it in this way, I get several graphs, and by some of them, after I have updated the data series, the origin is no longer in the middle of the graph but lays on the left side and I want to push it back in the middle – Claude G Nov 30 '16 at 13:55
  • @ClaudeG, I get you. Try the final snippet in my answer, it will allow you to set the axis as you wish based on values in A1, A2 – Preston Nov 30 '16 at 14:12