excel - 更改散点图vba excel中特定点的颜色

标签 excel vba colors scatter

我想更改散点图左上角部分的点的颜色。 我写了一个宏,没有发生错误,但颜色没有改变:/

Sub Kolorowanie()
ActiveSheet.ChartObjects("Chart 1").Activate
a = ActiveChart.SeriesCollection(1).Values
b = ActiveChart.SeriesCollection(1).XValues

For i = LBound(a) To UBound(a)

If a(i) < 0 And b(i) > 0 Then
  ActiveSheet.ChartObjects("Chart 1").Activate
  ActiveChart.SeriesCollection(1).Select
  ActiveChart.SeriesCollection(1).Points(i).Select
   With Selection.Format.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
    .Solid
   End With

Else
End If
Next i
End Sub

有什么想法为什么它不起作用吗?

最佳答案

摆脱.Solid或将其之前设置Forecolor.RGB,我认为这是覆盖某些东西(我有注意到这个和其他一些错误/无法执行某些操作,即使宏记录器会让这些方法看起来应该正常工作)。

Option Explicit
Sub Kolorowanie()
Dim cht As Chart
Dim srs As Series
Dim pt As Point

Set cht = ActiveSheet.ChartObjects(1).Chart

Set srs = cht.SeriesCollection(1)

For Each pt In srs.Points
    With pt.Format.Fill
        .Visible = msoTrue
        '.Solid  'I commented this out, but you can un-comment and it should still work
        .ForeColor.RGB = RGB(255, 0, 0)

    End With
Next


End Sub

或者:

For Each pt In srs.Points
With pt.Format.Fill
    .Visible = msoTrue
    .Solid  'This is the default so including it doesn't do anything, but it should work either way.
    .ForeColor.RGB = RGB(255, 0, 0)

End With
Next

将其应用到您的代码中可以为我们提供:

Sub Kolorowanie()
ActiveSheet.ChartObjects("Chart 1").Activate
a = ActiveChart.SeriesCollection(1).Values
b = ActiveChart.SeriesCollection(1).XValues

For i = LBound(a) To UBound(a)

If a(i) < 0 And b(i) > 0 Then
   ActiveSheet.ChartObjects("Chart 1").Activate
   ActiveChart.SeriesCollection(1).Select
   ActiveChart.SeriesCollection(1).Points(i).Select
   With Selection.Format.Fill
        .Visible = msoTrue
        .Solid
        .ForeColor.RGB = RGB(255, 0, 0)

   End With

Else
End If
Next i
End Sub

关于excel - 更改散点图vba excel中特定点的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15981802/

相关文章:

python - 在 python 中进行 Excel 格式化而不加载工作簿

excel - 将公式添加到 Excel VBA 表单保存按钮

html - 需要帮忙。基本的 HTML/CSS。标题不变色

java - 如何在 Java 文本编辑器中为关键字着色?

excel - 使用 ActiveX 表单控件避免 .Activate

vba - VBA 代码的 HTML Tidy 吗?

excel - 自动向下填充到最后一个相邻单元格的宏

vba - 将非空白单元格复制到下面的单元格中,对每个空白单元格重复

database - 创建前端 MDE

html - 悬停或访问时链接不会样式化