Programmatically add lines to chart - why do they appear in different locations?

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:

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!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I determined that the two arrows are 4 points different, both top and left. In other words, the arrow created within the ChartObject is 4 points to the right and 4 points below the arrow created outside the ChartObject.
 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

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