Excel-VBA - 如果LIKE *值*,颜色X轴点

时间:2012-03-02 12:10:09

标签: excel excel-vba vba

基本上我有一个从Java动态创建的图表(使用POI),我传递的值允许特定点在图表中着色。

为此,我需要访问点值标签,以便我可以测试条件属性是否适用于每个点值。

例如,我为variablesPointObject设置了变量

  1. 系列名称
  2. 点值名称
  3. 条件
  4. 颜色
  5. 我的伪代码如下

       For every seriesPointObject in the list
            Get all Values from Obj
            For Each series in the series collection
                 Get Every point
                      For every point label
                          Check condition with point value
                              if condition test is true 
                                     series point change colour
    

    但是我无法访问每个系列的点值标签。点值标签和系列之间必须存在联系,但我无法找到它。

    有什么方法可以从系列对象中获取点标签文本吗?

2 个答案:

答案 0 :(得分:3)

这样的事情可以解决问题

我有点惊讶我可以通过VBA访问每个图表系列的每个Point,但Point没有直接值。解决方法是将整个图表系列转储到变量数组中,测试数组中的每个值是否超出测试条件,然后使用Point

格式化chrSeries.Points(lngCnt)
Sub FormatPoints()
    Dim chr As ChartObject
    Dim chrSeries As Series
    Dim X As Variant
    Dim lngCnt As Long
    Set chr = ActiveSheet.ChartObjects(1)
    For Each chrSeries In chr.Chart.SeriesCollection
        X = chrSeries.Values
        For lngCnt = 1 To UBound(X)
            If X(lngCnt) > 10 Then
                With chrSeries.Points(lngCnt)
                    .MarkerBackgroundColor = vbRed
                    .MarkerForegroundColor = vbBlue
                End With
            End If
        Next
    Next
End Sub

sample

答案 1 :(得分:2)

  
    
      
        

在上面的示例中,它完美地运行,但是,如果我想针对a,b,c和d进行测试,该怎么办? to say:if(pointLabel ==“a”){编辑颜色的点}我认为在我的问题中点标签和刻度标签之间有一点混淆,因为我想访问相关的x轴上的标签到了系列的重点。

      
    
  

Hello Colin

要访问数据值或数据点的点标签,您必须首先遍历每个数据点,然后检索值。

Dave已经为您提供了一种检索Y值的方法。这是另一种可以获得X值和Y值的方法。

Sub FormatPoints()
    Dim chr As ChartObject
    Dim chrSeries As Series
    Dim X() As String
    Dim lngCnt As Long
    Dim pnt As Point
    Set chr = ActiveSheet.ChartObjects(1)


    For Each chrSeries In chr.Chart.SeriesCollection
        For Each pnt In chrSeries.Points
            pnt.DataLabel.ShowCategoryName = True
            X = Split(pnt.DataLabel.Caption, ",")

            '---- X Value ---------
            '~~> This will give you "A" for the above example
            '~~> which you can use for comparision
            Debug.Print X(0)

            '---- Y Value ---------
            '~~> This will give you 1
            Debug.Print X(1) ' OR

            pnt.DataLabel.ShowCategoryName = False
        Next
    Next
End Sub

修改

如果数据点不可见,上述代码将失败。您也可以使用此代码。

Sub FormatPoints()
    Dim chr As ChartObject
    Dim chrSeries As Series
    Dim X() As String
    Dim lngCnt As Long
    Dim pnt As Point
    Set chr = ActiveSheet.ChartObjects(1)


    For Each chrSeries In chr.Chart.SeriesCollection
        For Each pnt In chrSeries.Points
            '~~> You need this line else the code will fail
            pnt.DataLabel.ShowValue = True

            pnt.DataLabel.ShowCategoryName = True
            X = Split(pnt.DataLabel.Caption, ",")
            pnt.DataLabel.ShowCategoryName = False

            MsgBox "X Value :" & X(0) & vbNewLine & "Y Value :" & X(1)
        Next
    Next
End Sub

<强>快照

enter image description here

现在如果您将X轴值设为“ Sid,Rout ”,则上述操作无效。对于这些场景,我创建了一个额外的功能。请参阅下面的代码。

Sub FormatPoints()
    Dim chr As ChartObject
    Dim chrSeries As Series
    Dim X As String, Y As String
    Dim lngCnt As Long
    Dim pnt As Point
    Set chr = ActiveSheet.ChartObjects(1)


    For Each chrSeries In chr.Chart.SeriesCollection
        For Each pnt In chrSeries.Points
            '~~> You need this line else the code will fail
            pnt.DataLabel.ShowValue = True

            pnt.DataLabel.ShowCategoryName = True

            X = GetVal(pnt.DataLabel.Caption, "X")
            Y = GetVal(pnt.DataLabel.Caption, "Y")

            pnt.DataLabel.ShowCategoryName = False

            MsgBox "X Value :" & X & vbNewLine & "Y Value :" & Y
        Next
    Next
End Sub

Function GetVal(DataPointCaption As String, strAxis As String) As String
    Dim TempAr() As String

     TempAr = Split(DataPointCaption, ",")

     If strAxis = "Y" Then GetVal = TempAr(UBound(TempAr))
     If strAxis = "X" Then
        For i = LBound(TempAr) To (UBound(TempAr) - 1)
            GetVal = GetVal & "," & TempAr(i)
        Next i
        GetVal = Mid(GetVal, 2)
     End If
End Function

<强>快照

enter image description here

HTH

西特