DeckMaster
New Member
- Joined
- Jun 15, 2011
- Messages
- 3
Hello,
I am trying to add arrows below a chart based on dates along the x-axis using vba. Using Application.WorksheetFunction.Match on the raw data, I am able to get the serial position of the point on the chart. Then using SeriesCollection(...).Points(...).Left gives me the horizontal location. This works great if I create the arrow within the ChartObject. However, if I create arrow outside the ChartObject, it is just slightly off, no matter where the chart is located within the sheet (the delta is always the same) - and the final position must be exact. The reason I need it created as a shape separate from the ChartObject is that I would like to be able to manipulate the arrow using the arrow keys, and this appears to be impossible for shapes created within charts.
Note that when creating the arrow outside the ChartObject, I add the .Left and .Top position of the ChartObject itself to account for the position of the chart within the sheet.
Here is my code:
I can't for the life of me figure out why these two arrows do not appear in exactly the same place. Any help would be greatly appreciated. Thank you!
I am trying to add arrows below a chart based on dates along the x-axis using vba. Using Application.WorksheetFunction.Match on the raw data, I am able to get the serial position of the point on the chart. Then using SeriesCollection(...).Points(...).Left gives me the horizontal location. This works great if I create the arrow within the ChartObject. However, if I create arrow outside the ChartObject, it is just slightly off, no matter where the chart is located within the sheet (the delta is always the same) - and the final position must be exact. The reason I need it created as a shape separate from the ChartObject is that I would like to be able to manipulate the arrow using the arrow keys, and this appears to be impossible for shapes created within charts.
Note that when creating the arrow outside the ChartObject, I add the .Left and .Top position of the ChartObject itself to account for the position of the chart within the sheet.
Here is my code:
Code:
<code>Dim SampleChart As ChartObject
Dim Arrow as Shape
Dim DataPoint
Dim varDate
varDate = 1/1/2011
Set SampleChart = Sheets(1).ChartObjects(1)
DataPoint = Sheets("Raw Data").Application.WorksheetFunction.Match(varDate, Sheets("Raw Data").Range("A1").Column, 0)
'This...:
Set Arrow = SampleChart.Chart.Shapes.AddConnector(msoConnectorStraight, _ BeginX:=SampleChart.Chart.SeriesCollection(1).Points(DataPoint).Left, _
BeginY:=SampleChart.Chart.Axes(xlCategory).Top + 50, _
EndX:=SampleChart.Chart.SeriesCollection(1).Points(DataPoint).Left, _
EndY:=SampleChart.Chart.Axes(xlCategory).Top + 20)
Arrow.Line.EndArrowheadStyle = msoArrowheadOpen
'...is not located in the same position as this:
Set Arrow = Sheets(1).Shapes.AddConnector(msoConnectorStraight, _
BeginX:=SampleChart.Left + SampleChart.Chart.SeriesCollection(1).Points(DataPoint).Left, _
BeginY:=SampleChart.Top + SampleChart.Chart.Axes(xlCategory).Top + 50, _
EndX:=SampleChart.Left + SampleChart.Chart.SeriesCollection(1).Points(DataPoint).Left, _
EndY:=SampleChart.Top + SampleChart.Chart.Axes(xlCategory).Top + 20)
Arrow.Line.EndArrowheadStyle = msoArrowheadOpen
</code>
I can't for the life of me figure out why these two arrows do not appear in exactly the same place. Any help would be greatly appreciated. Thank you!