如何从存储在series.xvalues中的一系列单元格中访问单个单元格?

时间:2017-12-07 17:25:52

标签: excel vba excel-vba

为什么我尝试这样做的背景故事是因为我需要在局部最大值的峰值处放置标注标签,但值必须是x值而不是y值。

我尝试过多种方式,最近最喜欢这样:

Dim c As Chart

Set c = sht.Shapes.AddChart.Chart

With c
    .ChartType = xlXYScatterLinesNoMarkers
    .SeriesCollection(8).Name = "Data Labels"
    .SeriesCollection(8).XValues = Range(sht.Cells(start, 2), sht.Cells(ender, 2))
    .SeriesCollection(8).Values = Range(sht.Cells(start, 9), sht.Cells(ender, 9))
    .SeriesCollection(8).Format.Line.Visible = msoFalse
End With

For i = 1 To c.SeriesCollection(8).Points.count

    c.SeriesCollection(8).Points(i).HasDataLabel = True
    c.SeriesCollection(8).Points(i).DataLabel.Position = xlLabelPositionAbove
    c.SeriesCollection(8).Points(i).DataLabel.Format.AutoShapeType = msoShapeRectangularCallout
    c.SeriesCollection(8).Points(i).DataLabel.Format.Line.Visible = msoTrue
error--->c.SeriesCollection(8).Points(i).DataLabel.Text = c.SeriesCollection(8).XValues.Cells(i, 1)

Next i

这将返回错误" Object Required"

我也试过

c.SeriesCollection(8).Points(i).DataLabel.Text = c.SeriesCollection(8).XValues(i)
c.SeriesCollection(8).Points(i).DataLabel.Text = c.SeriesCollection(8).XValues(i).value

还有一些我最有可能忘记了我的头脑。

我真正想要和需要的是检索位于" i" c.SeriesCollection(8).XValues

的位置

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

XValues返回Variant数组(不是Range),但由于某种原因会抛出错误:

Debug.Print ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).XValues(1)
  

运行时错误'':属性let过程未定义和属性   get procedure没有返回对象

虽然这很好用:

Sub Tester()

    Dim v, i
    v = ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).XValues

    For i = LBound(v) To UBound(v)
        Debug.Print "Point#" & i, v(i)
    Next i


End Sub
相关问题