2

I am trying to make a macro that will make a chart for me out of two columns of data. I keep coming across this error no matter what I do. I've attached an image here.

enter image description here

braX
  • 11,506
  • 5
  • 20
  • 33
  • 1
    Please paste your code as text, not as an image: it's much easier to suggest a fix when we can copy/paste and then edit. Otherwise we have to type the whole thing out. It's likely though that your just-inserted chartobject is not name "Chart13" but something else. – Tim Williams May 03 '20 at 20:59

2 Answers2

0

your macro performs the process up to the line where it moves the graph to the right In the next statement, move the chart up more points than the sheets allow. From the default position in which Excel inserts a graph there are only 47 points up. Try changing the value "-94.8" for one closer to 0, and you will see that your macro now runs.

For example this macro runs normally moving the graph up 47 points ("IncrementTop -47.01") But if the value changed by 90 points, the macro gives an error

Sub ProbingChart()
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
Application.CutCopyMode = False
ActiveChart.SetSourceData Source:=Sheets("data").Range("A1:A12,D1:D12")
ActiveSheet.Shapes("Gráfico 1").IncrementLeft 0.75
ActiveSheet.Shapes("Gráfico 1").IncrementTop -47.01
ActiveSheet.Shapes("Gráfico 1").IncrementLeft 244.5
ActiveSheet.Shapes("Gráfico 1").IncrementLeft -559.5
ActiveSheet.Shapes("Gráfico 1").IncrementTop 102.75
Range("A1").Select
End Sub

I hope I've helped

regards

MarvikDC
  • 16
  • 1
0

The macro recorder typically writes terrible code - it's good for finding out the property or method you want to use, but you're nearly always better off rewriting what it creates.

This for example is neater and easier to modify:

Dim shp As Shape, cht As Chart

'get a reference to the added shape
Set shp = ActiveSheet.Shapes.AddChart2(240, xlXYScatter)
shp.Left = 100  '<< better than using IncrementXXX methods
shp.Top = 100
shp.Height = 300
shp.Width = 400

'get a reference to the contained Chart object
Set cht = shp.Chart
cht.SetSourceData Sheets("Sheet6").Range("F10:F41,H10:H41")
cht.SetElement msoElementPrimaryCategoryAxisTitleAdjacentToAxis
cht.SetElement msoElementPrimaryValueAxisTitleAdjacentToAxis
Tim Williams
  • 154,628
  • 8
  • 97
  • 125