使用VBA更改XY散点图上的线条颜色

时间:2020-01-27 14:14:29

标签: excel vba

我希望根据另一个工作表中单元格的值来更改每个系列的颜色。

我通过逐步检查宏进行了检查,除颜色变化以外的所有内容均正常工作。对于不同类型的事件,这种类型的代码将运行六次(为了缩短本文的长度,我删除了五个事件)。

在任何系列中我都没有标记,只有该系列两点之间的连接线。

图形的屏幕抓图。
enter image description here

Sub test()

    Dim LastRowB As Long
    Dim LastRowJ As Long
    Dim LastRowR As Long
    Dim LastRowZ As Long
    Dim LastRowAH As Long
    Dim LastRowAP As Long
    Dim chartj As Variant

    Dim wb As Workbook
    Dim ws As Worksheet
    Set ws6 = Sheet6

'Set chart axis to approximately the range required

    Chart7.Axes(xlCategory).MinimumScale = 43000
    Chart7.Axes(xlCategory).MaximumScale = 44500

'Set graph ranges as per sheet

    Chart7.Axes(xlCategory).MinimumScale = ws6.Range("D9").Value
    Chart7.Axes(xlCategory).MaximumScale = ws6.Range("D10").Value

'All subsequent examples follow template for fills

'--------------------------- Fill ------------------------------

'Find first occurence of -. There should be a - under all arrays to ensure that a stop ocurs or this will get stuck in a loop if all entries are filled
'i.e. 15 fill batches are performed and no - will appear in the table.

    LastRowB = ws6.Cells.Find(What:="-", _
                    After:=ws6.Range("B15"), _
                    LookAt:=xlWhole, _
                    LookIn:=xlValues, _
                    searchorder:=xlByColumns, _
                    searchdirection:=xlNext, _
                    MatchCase:=False).Row

' Set the integers  j = 1 and runs all loops up until a max of 15 occurences based off of i. This is cause there are 15 Fills series plotted on the graph.
' i = 16 as arrays start in row 16. And this runs until the number of row that the last dash is found.

    Dim i As Integer
    Dim j As Integer
    j = 1

    For i = 16 To LastRowB
        If i = LastRowB Then
            'if we have hit the last row number then we end the if and move on
        Else
            Set chartj = Chart7.SeriesCollection(j)
            chartj.Select
            'This .select is to confirm that the correct series is being selected. Can be removed.
            'Check to see if the series should be green or red colour from the colour column
            If ws6.Range("F" & i).Value = "Green" Then

                'Format the connecting lines as green
                chartj.Format.Line.Visible = msoFalse
                chartj.Format.Line.Visible = msoTrue
                chartj.Border.LineStyle = xlContinuous
                chartj.Border.Color = RGB(0, 176, 80)

            Else

                'Format the connecting lines as red
                chartj.Format.Line.Visible = msoFalse
                chartj.Format.Line.Visible = msoTrue
                chartj.Border.LineStyle = xlContinuous
                chartj.Border.Color = RGB(255, 0, 0)

            End If
        End If
        j = j + 1
    Next i

Windows 10中的Excel 2013。

1 个答案:

答案 0 :(得分:0)

尝试

chartj.Format.Line.ForeColor.RGB = RGB(0, 176, 80)

希望有帮助