【发布时间】:2020-07-04 04:08:35
【问题描述】:
【问题讨论】:
-
仅供参考,MSDN 中的文档非常棘手。不过看了一遍,提交了一个不完整的答案后,我终于想到了用 Excel 中的记录器,然后修改为 PowerPoint,这就是我找到最终解决方案的地方。值得知道,以防您将来遇到其他类似问题。
标签: vba powerpoint
【问题讨论】:
标签: vba powerpoint
实现这一点的代码如下:
Dim myChart As Chart
Dim mySerCol As SeriesCollection
Dim strRange As String
strRange = "=Sheet1!$F$2:$F$5" 'To hold the range for the new labels
Set myChart = ....[put in code to get the appropriate chart]
Set mySerCol = myChart.SeriesCollection(i)
mySerCol.ApplyDataLabels 'Turn on the datalabels for this series
'The next line sets the range to get the values from
mySerCol.Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange _
, strRange, 0
mySerCol.ShowRange = True 'Show the values from the range
mySerCol.ShowValue = False 'Do not show the actual values of the points
请注意,这只会对其中一个系列执行此操作。要执行其他操作,请遍历 myChart.SeriesCollections(i) 行中的 i。
【讨论】:
****EDIT**** 查看其他答案。我把它留在这里是因为它提供了一些关于可以使用的几个对象的信息,但实际上并没有解决问题。
这不是对该问题的完整答案,但对于评论来说太长了。
我已经搜索了 Datalabels 的文档,但无法弄清楚如何执行此操作(我假设您希望能够使用 VBA 定义标签的来源范围)。我能够“选中”复选框,但无法弄清楚附加到它的范围在哪里。要检查的相应代码。
要选中复选框,请使用以下代码:
myChart.SeriesCollection(i).ApplyDataLabels
其中 i 是相关系列,myChart 是引用图表的 Chart 对象。这个方法有一堆参数可以让你显示不同的项目(百分比、值等),但没有一个参数是范围。
如果您不输入任何可选参数,则默认为系列的值
然后可以使用以下方法打开和关闭它:
myChart.SeriesCollection(i).DataLabels.ShowRange = True/False
可以使用以下方法更改数据标签的标题:
myChart.SeriesCollection(i).DataLabels(j).Caption = "MY CAPTION"
这将一次更改一个标题,并将替换“ApplyDataLabels”方法放入其中的值。可以循环遍历范围来设置值,但这可能不是您想要的。
还有这个:
myChart.SeriesCollection(i).HasDataLabels = True
但这似乎只是打开和关闭它们并重置您可能已放入其中的标题。
MSDN 链接同时使用了 hasdatalabels 属性和 applydatalabels 方法,但不清楚它们为什么同时使用:https://msdn.microsoft.com/EN-US/library/office/ff745965.aspx
希望这至少可以为您提供一些帮助。
【讨论】: