在上面的示例中,它完美地工作,但是,如果我想測試a,b,c和d,該怎麼辦。說:if(pointLabel ==「a」){編輯點的顏色}我認爲在我的問題點標籤和刻度標籤之間有點混亂,因爲我想訪問相關的x軸上的標籤到系列的點。
您好科林
要訪問datavalue或數據點的點標籤,您通過每個數據點必須先循環,然後檢索值。
戴夫已經給你一個方法來檢索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軸值 「希德,潰敗」 那麼上面將無法正常工作。對於這些場景,我創建了一個額外的功能。請參閱下面的代碼。
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
希德
優秀的回覆。正是我在找什麼。謝謝! – 2012-03-09 09:28:36