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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15981802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:23:52  来源:igfitidea点击:

Changing the colors of the specific dots in scatterplot vba excel

excelvbacolorsscatter

提问by haver24

I would like to change the color of dots in top left part of the scatterplot. I wrote a macro, no errors occurred but the color did not change :/

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

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

Any ideas why it does not work?

任何想法为什么它不起作用?

回答by David Zemens

Get rid of the .Solidor put this beforeyou set the Forecolor.RGB, I think that is overriding something (I have noticed this and some other bugs/inabilities to perform certain actions even as the Macro Recorder would make it seem these methods should work properly).

设置之前摆脱.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

Or:

或者:

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

Applying this to your code gives us:

将此应用于您的代码可以为我们提供:

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