我正在尝试根据与X或Y不同的值更改散点图中的标记颜色。该值与标记的X和Y位于同一行。 我已经这样做了,但我似乎无法正确定义值区域。 我不是真的编码,所以我可能会以错误的方式看待它...
Sub color_chart()
Dim chartIterator As Integer, pointIterator As Integer, _
seriesArray() As Variant
For chartIterator = 1 To ActiveSheet.ChartObjects.Count
seriesArray = ActiveWorkbook.Sheets("Graph").ChartObjects(chartIterator). _
Chart.SeriesCollection(1).Values
For pointIterator = 1 To UBound(seriesArray)
Dim value As Range
Set value = Worksheets("Ranking").Range("F6", "F11")
Dim valueR As Integer
Set valueR = value.Cells.Item(1, pointIterator)
If valueR(pointIterator) = 0 Then
ActiveWorkbook.Sheets("Sheet1").ChartObjects(chartIterator). _
Chart.SeriesCollection(1).Points(pointIterator).Interior.Color = _
RGB(255, 0, 0)
ElseIf valueR(pointIterator) = 1 Then
ActiveWorkbook.Sheets("Sheet1").ChartObjects(chartIterator). _
Chart.SeriesCollection(1).Points(pointIterator).Interior.Color = _
RGB(255, 102, 102)
ElseIf valueR(pointIterator) = 2 Then
ActiveWorkbook.Sheets("Sheet1").ChartObjects(chartIterator). _
Chart.SeriesCollection(1).Points(pointIterator).Interior.Color = _
RGB(255, 128, 0)
ElseIf valueR(pointIterator) = 3 Then
ActiveWorkbook.Sheets("Sheet1").ChartObjects(chartIterator). _
Chart.SeriesCollection(1).Points(pointIterator).Interior.Color = _
RGB(255, 255, 0)
ElseIf valueR(pointIterator) = 4 Then
ActiveWorkbook.Sheets("Sheet1").ChartObjects(chartIterator). _
Chart.SeriesCollection(1).Points(pointIterator).Interior.Color = _
RGB(128, 255, 0)
ElseIf valueR(pointIterator) = 5 Then
ActiveWorkbook.Sheets("Sheet1").ChartObjects(chartIterator). _
Chart.SeriesCollection(1).Points(pointIterator).Interior.Color = _
RGB(0, 255, 0)
End If
Next pointIterator
Next chartIterator
End Sub
答案 0 :(得分:1)
您已将ValueR
声明为整数,因此您无法使用Set
关键字来指定其值。此外,您可能希望使用Select Case
而不是嵌套的If...ElseIf...
语句,这将更清晰。我还使用With
块来删除对图表/系列/点对象的一些冗余调用:
With ActiveWorkbook.Sheets("Sheet1").ChartObjects(chartIterator). _
Chart.SeriesCollection(1).Points(pointIterator).Interior
Select Case ValueR
Case Is = 0
.Color = _
RGB(255, 0, 0)
Case 1
.Color = _
RGB(255, 102, 102)
Case 2
.Color = _
RGB(255, 128, 0)
Case 3
.Color = _
RGB(255, 255, 0)
Case 4
.Color = _
RGB(128, 255, 0)
Case 5
.Color = _
RGB(0, 255, 0)
End Select
End With