简体   繁体   English

Excel图表(VBA)中的点

[英]Points in Excel Charts (VBA)

Is there a way in VBA to identify a point in a chart with something else than its number reference? 在VBA中,有没有一种方法可以用其他数字标识来标识图表中的某个点?

When placing the pointer above a section of the chart, it displays the serie number and also some narrative. 将指针放在图表的某个部分上方时,它会显示意向号和一些叙述。 Is this the "Name" of the point? 这是要点的“名称”吗?

Ex: Serie 1 Point " Europe " Value: 12 (51%) 例:意甲1分“ 欧洲 ”价值:12(51%)

I am trying to move away from: 我正试图摆脱:

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

and writing: 和写作:

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

The name does not refer to the point, that has a given name such as S1P1. 该名称不指向具有给定名称的点,例如S1P1。

What you could do is to store the names and indexes of the XValues in a collection, then use that 您可以做的是将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

There may be a better way to do this, but AFAIK and remember from my experience, this is the only way to test for Category Axis Labels when looping through charts and act upon the series based on the Category Axis Label. 可能有更好的方法,但是AFAIK并请记住我的经验,这是在遍历图表并基于“类别轴标签”对系列进行操作时测试“类别轴标签”的唯一方法。

I have commented the code heavily in order to make it as clear as possible: 为了使代码尽可能清晰,我对代码进行了大量注释:

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