繁体   English   中英

Excel图表(VBA)中的点

[英]Points in Excel Charts (VBA)

在VBA中,有没有一种方法可以用其他数字标识来标识图表中的某个点?

将指针放在图表的某个部分上方时,它会显示意向号和一些叙述。 这是要点的“名称”吗?

例:意甲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。

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

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$3,Sheet1!$E$4:$E$8,Sheet1!$F$4:$F$8,1) as example

Dim rLabel As Range, cel As Range
Set rLabel = Range(aForm(1)) 'set the label range (Sheet1!$E$4:$E$8 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM