【问题标题】:Changing line color on a XY scatter graph with VBA使用 VBA 更改 XY 散点图上的线条颜色
【发布时间】:2020-01-27 14:14:29
【问题描述】:

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

我通过单步执行宏进行了检查,除了颜色更改之外的所有内容都正常工作。这种类型的代码将针对不同类型的事件运行六次(我删除了其中的五个事件以缩短本文的篇幅)。

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

图表的屏幕截图。

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。

【问题讨论】:

  • chartj.Format.Line.Visible = msoFalse 后面紧跟chartj.Format.Line.Visible = msoTrue 的目的是什么,为什么这些行会出现在If 语句的两个分支中?
  • 感谢您的回复 jsheeran。我发现另一篇文章解释了一些关于在 excel 系列here 上调整颜色的问题。它描述了使用 .visible=msoFalse 和 .visible=msoTrue 语句欺骗系统的需要。正如我所指出的,我试图根据自己的需要调整此代码,但不幸的是无法让事情正常工作。

标签: excel vba


【解决方案1】:

试试

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

希望有帮助

【讨论】:

  • 不幸的是我以前试过这个。再次输入工作表以查看它是否有效,但它没有。不过感谢您的回答。
  • 你能更详细地解释一下它是如何不工作的吗?它会抛出错误吗?结果与您的预期有何不同?
  • 代码按预期运行,直到行颜色更改,即此代码 chartj.Format.Line.Visible = msoFalse chartj.Format.Line.Visible = msoTrue chartj.Border.LineStyle = xlContinuous chartj.Border.Color = RGB(0, 176, 80) 从我的错误检查中选择了正确的格式化系列,但在逐步执行此操作时未发生任何操作(即颜色更改)代码区域。
猜你喜欢
  • 2015-04-17
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 2014-09-24
  • 2021-10-05
  • 1970-01-01
  • 2022-12-12
相关资源
最近更新 更多