Excel 从图形中获取VBA变量的值

Excel 从图形中获取VBA变量的值,excel,vba,Excel,Vba,我试图使用VBA将数据标签仅添加到饼图的最大部分 当我尝试运行以下代码时,会收到一条错误消息: '对象不支持方法的此属性' 该错误与以下行有关: sem = ActiveSheet.ChartObjects("REJT").Chart.SeriesCollection(1).Points(x).Value 有人能告诉我这条线有什么问题吗? 是否有其他方法获取图表数据点的值 Dim krb As Long Dim x As Long Dim rr As Long Dim sem As Long

我试图使用VBA将数据标签仅添加到饼图的最大部分

当我尝试运行以下代码时,会收到一条错误消息: '对象不支持方法的此属性'

该错误与以下行有关:

sem = ActiveSheet.ChartObjects("REJT").Chart.SeriesCollection(1).Points(x).Value
有人能告诉我这条线有什么问题吗? 是否有其他方法获取图表数据点的值

Dim krb As Long
Dim x As Long
Dim rr As Long
Dim sem As Long
Dim xmax As Long

Set pts = ActiveSheet.ChartObjects("REJT").Chart.SeriesCollection(1).Points
krb = pts.Count
x = 1
rr = 0

'While loop to find largest part of pie chart
While x < (krb + 1)
sem = ActiveSheet.ChartObjects("REJT").Chart.SeriesCollection(1).Points(x).Value
MsgBox (sem)
If sem > rr Then
rr = sem
xmax = x
End If

x = x + 1
Wend

'Add data label to the largest part of pie chart
ActiveSheet.ChartObjects("REJT").Activate
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Points(xmax).Select
ActiveChart.SeriesCollection(1).Points(xmax).ApplyDataLabels  
Dim krb尽可能长
暗x等长
长时变暗
如长
Dim xmax尽可能长
设置pts=ActiveSheet.ChartObjects(“REJT”).Chart.SeriesCollection(1).Points
krb=分计数
x=1
rr=0
'同时循环查找饼图的最大部分
而x<(krb+1)
sem=ActiveSheet.ChartObjects(“REJT”).Chart.SeriesCollection(1).Points(x).Value
MsgBox(sem)
如果sem>rr,则
rr=sem
xmax=x
如果结束
x=x+1
温德
'将数据标签添加到饼图的最大部分
图表对象(“REJT”).激活
ActiveChart.SeriesCollection(1)。选择
ActiveChart.SeriesCollection(1).Points(xmax).选择
ActiveChart.SeriesCollection(1).Points(xmax).ApplyDataLabels

以下是从饼图中获取最大点值并将数据标签应用于其位置的一种方法:

Sub pieChartMaxDataLabel()
    Dim cht As Chart
    Set cht = ActiveChart

    maxValue = WorksheetFunction.Max(cht.SeriesCollection(1).Values)
    maxPosition = WorksheetFunction.Match(maxValue, cht.SeriesCollection(1).Values, 0)
    cht.SeriesCollection(1).Points(maxPosition).ApplyDataLabels

    Debug.Print "Max Value is: "; maxValue; " At Position: "; maxPosition
End Sub

使用如下所示的图表值进行测试:

结果:

您是否尝试过与您的需求类似的方法。它将获得图表中的最大值。您只需添加到代码中,即可在找到数据标签时分配数据标签。