1

我正在尝试根据不同于 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
4

1 回答 1

1

您已声明ValueR为整数,因此您不能使用Set关键字为其赋值。此外,您可能希望使用 aSelect 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
于 2013-05-29T14:06:56.890 回答