Excel 图表中的点 (VBA)

Points in Excel Charts (VBA)

在 VBA 中有没有一种方法可以用数字参考以外的东西来识别图表中的点?

将指针放在图表的某个部分上方时,它会显示序列号和一些说明。这就是重点的"Name"吗?

例如:意甲 1 分“欧洲”值:12 (51%)

我正在尝试远离:

Worksheets(1).ChartObjects(1).Chart. _ SeriesCollection(1).Points(3).MarkerStyle = xlDiamond

并写作:

Worksheets(1).ChartObjects(1).Chart. _ SeriesCollection(1).Points("Europe").MarkerStyle = xlDiamond

名称不是指具有给定名称(例如 S1P1)的点。

您可以做的是将 XValue 的名称和索引存储在一个集合中,然后使用它

Dim myValues As Collection
Dim xv As Variant
Dim i As Long

    Set myValues = New Collection

    With Worksheets(1).ChartObjects(1).Chart.SeriesCollection(1)

        For Each xv In .XValues

            i = i + 1
            myValues.Add i, xv
        Next xv

        .Points(myValues.Item("Europe")).MarkerStyle = xlDiamond
    End With

可能有更好的方法来做到这一点,但 AFAIK 并记住我的经验,这是在循环遍历图表并根据类别轴标签对系列进行操作时测试类别轴标签的唯一方法。

我对代码进行了大量注释,以使其尽可能清晰:

Sub LoopAxisLabels()

Dim ws As Worksheet
Dim c As ChartObject
Dim sc As SeriesCollection

Set ws = Worksheets(1)
Set c = ws.ChartObjects(1)
Set sc = c.Chart.SeriesCollection

Dim aForm() As String
aForm() = Split(sc.Item(1).Formula, ",")
'^^^
'|||get formula (chart data source) for the series collection (Item(1) denotes first series collection on chart
'   assumes only 1 collection on chart)
'   places formula into array separate by comma, based on =SERIES(Sheet1!$F,Sheet1!$E:$E,Sheet1!$F:$F,1) as example

Dim rLabel As Range, cel As Range
Set rLabel = Range(aForm(1)) 'set the label range (Sheet1!$E:$E in the example)

Dim i As Long

For Each cel In rLabel 'loop through actual cells of label and test for label values

    i = i + 1 'use counter to refer to specific chart points (since points will be in order of data listed in range

    Select Case cel.Value2

        Case Is = "Europe": sc.Item(1).Points(i).MarkerStyle = xlDiamond 'Points(i) matches the relative range reference

        Case Is = "...":

        'continue with other Cases...

    End Select

Next

End Sub