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

基本上我有一个从Javadynamic创build的图表(使用POI),我正在通过允许特定点在图表中着色的值。

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

例如,我有variables设置为一个seriesPointObject

  1. 系列名称
  2. 点值名称
  3. 条件
  4. 颜色

我的伪代码如下

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 

但是我无法访问每个系列的点值标签。 点值标签和系列之间必须有连接,但我无法find它。

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

在你的上面的例子中,它完美的工作,但是,如果我想testinga,b,c和d的话。 说:如果(pointLabel ==“a”){编辑点的颜色}我认为在我的问题点标签和刻度标签之间有点混乱,因为我想访问相关的x轴上的标签到系列的点。

科林,你好

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

戴夫已经给你一个方法来检索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 

快照

在这里输入图像说明

现在,如果你的X轴的值是“ Sid,Rout ”,那么上面的就不行了 。 对于这些场景,我创build了一个额外的function。 请参阅下面的代码。

 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 

快照

在这里输入图像说明

HTH

希德

像这样的事情会做的伎俩

我有点惊讶,我可以通过VBA访问每个图表系列的每个Point ,但是这个Point没有直接的价值。 解决方法是将整个图表系列转储到variables数组中,testing数组中每个值的条件是否超过testing,然后使用chrSeries.Points(lngCnt)格式化该Point

 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 

样品