数据标签的Excel XY图表坐标通过多个图表模板循环

任何人都可以帮助我调整以下代码来循环两个数据序列的数据点的范围? 还帮我循环在同一工作簿中的不同工作表上的多个图表? 还有一种方法可以将不规则的.Left .Left = XVal赋值给计算的单元格,以便根据轴/显示的数据点的数量自动进行调整。

这是一个XY图表 – 我在这里遇到了代码,它获取了数据点的坐标,并将数据标签放置在数据序列的上方。 我在每个图表上有两个数据系列,我需要使用这个系列 – SeriesCollection 5和SeriesCollection 3.我已经使用增量.Left = XVal增量值,以便数据标签在图表顶部顺序排列。

 Public Sub ChangeCoordinates() Dim cht As Excel.Chart Dim srs As Excel.Series Dim i As Long Set cht = Application.ActiveChart Set srs = cht.SeriesCollection(5) For i = 1 To srs.Points.Count XVal = ExecuteExcel4Macro("GET.CHART.ITEM(1,1,""S1P" & i & """)") YVal = ExecuteExcel4Macro("GET.CHART.ITEM(2,1,""S1P" & i & """)") With ActiveChart.SeriesCollection(5).Points(1).DataLabel .Left = XVal - 550 .Top = YVal + 50 End With With ActiveChart.SeriesCollection(5).Points(2).DataLabel .Left = XVal - 500 .Top = YVal + 50 End With Next i End Sub 

要循环访问这两个数据系列,您可以简单地添加一个循环:

 Public Sub ChangeCoordinates() Dim cht As Chartobject Dim srs As Excel.Series Dim h as Long Dim i As Long Set cht = Application.ActiveChart For h = 3 to 5 step 2 Set srs = cht.SeriesCollection(h) For i = 1 To srs.Points.Count XVal = ExecuteExcel4Macro("GET.CHART.ITEM(1,1,""S1P" & i & """)") YVal = ExecuteExcel4Macro("GET.CHART.ITEM(2,1,""S1P" & i & """)") With ActiveChart.SeriesCollection(h).Points(i).DataLabel 'replaced points(1) with points(i) .Left = XVal - 550 .Top = YVal + 50 End With Next i Next h End Sub 

要循环遍历所有的工作表,以及这些工作表上的图表,只需添加更多的循环:)

 Public Sub ChangeCoordinates() Dim cht As chartobject Dim srs As Excel.Series Dim shtcount as Long Dim chtcount as Long Dim f as long Dim g as Long Dim h as Long Dim i As Long shtcount = thisworkbook.worksheets.count For f = 1 to shtcount chtcount = thisworkbook.worksheets(f).chartobjects.count For g = 1 to chtcount Set cht = thisworkbook.worksheets(f).ChartObjects(g) For h = 3 to 5 step 2 Set srs = cht.Chart.SeriesCollection(h) For i = 1 To srs.Points.Count XVal = ExecuteExcel4Macro("GET.CHART.ITEM(1,1,""S1P" & i & """)") YVal = ExecuteExcel4Macro("GET.CHART.ITEM(2,1,""S1P" & i & """)") With srs.Points(i).DataLabel 'replaced points(1) with points(i) If h = 3 Then .Left = XVal - 550 .Top = YVal + 50 Else 'Insert other values for series 5 here End if End With Next i Next h Next g Next f End Sub