【问题标题】:How to address Marker in scatter plot via VBA?如何通过VBA解决散点图中的标记?
【发布时间】:2019-12-19 09:10:57
【问题描述】:

我正在尝试为散点图设置一个宏,您可以在其中切换图例中数据集的位置并格式化标记。

这是我的宏:

Sub FormatLegend

Dim ChtObj As ChartObject
Set ChtObj = Worksheets("Plot_1").ChartObjects("Diagramm 1")

With ChtObj

    With .Chart.SeriesCollection(1)
         .PlotOrder = 3
    End With

    With .Chart.SeriesCollection(4)
        .Format.Fill.Visible = msoTrue
        .Format.Line.Visible = msoFalse
        .Format.Fill.BackColor.RGB = RGB(146, 208, 80)
        .MarkerSize = 4
        .MarkerStyle = 2
        .Weight = 0.75
    End With
End Sub

我不明白如何区分线和标记。当我设置.Format.Line.Visible = msoFalse 时,整行都消失了。我希望标记保持可见。

如何使线不可见,而不是标记?此外,我想将标记的宽度设置为 0.75 并且不应用填充。

【问题讨论】:

  • Olli,我下面的解决方案有效吗?谢谢
  • 嗨@Alex L,抱歉我的回复延迟。你的方法确实有效,非常感谢!很好的提示 .Border.LineStyle = xlLineStyleNone
  • 太棒了! @奥利

标签: excel vba


【解决方案1】:

对我来说这是可行的:

Sub Add_colour_scale_to_scatter()

    chart_name = "Chart 2"

    Set my_chart = ActiveSheet.ChartObjects(chart_name).Chart
    Set my_series = my_chart.FullSeriesCollection(1)

    my_colour = RGB(0, 176, 80) 'RGB(146, 208, 80)
    Debug.Print my_colour

    For i = 1 To my_series.Points.Count
        my_series.Points(i).Select
        With Selection 'my_series.Points(i)
            .MarkerForegroundColor = my_colour 'Border colour of the points
            .MarkerBackgroundColor = my_colour 'Colour of the points themselves

            .Format.Line.Weight = 0 'border of the point 0pt
            .Format.Line.Visible = msoFalse 'border of the point not visible
            .Format.Line.Transparency = 1 'border of the point is completely transparent

            .Border.Color = my_colour 'colour of the line between points
            .Border.LineStyle = xlLineStyleNone 'line between points is none. Others: 'xlContinuous 'continous line 'xlDot 'dotted line

            .Format.Fill.Visible = msoTrue 'the point is visible
            .Format.Fill.Solid 'the point has a solid fill
            .Format.Fill.ForeColor.RGB = my_colour
            .Format.Fill.Transparency = 0.3

            .MarkerStyle = 8 'round points
            .MarkerSize = 5 'size of the points
        End With
        If i Mod 100 = 0 Then
            DoEvents
            'Debug.Print i
            Application.StatusBar = i & " of " & my_series.Points.Count
        End If

    Next i

    MsgBox "done"

End Sub

问题在于,当您录制宏时,它会为您提供相同的代码来更改点之间的线和更改点周围的线。

我相信.Format.Line.Visible = msoFalse 删除了点之间的线和点周围的线。这有点违反直觉。按此顺序进行操作并使用.Border.LineStyle = xlLineStyleNone 对我来说似乎是最好的方法。 (即首先使用.Format.Line.Visible = msoFalse 设置点之间和周围的线的边界,然后使用.Border.LineStyle = xlLineStyleNone 仅设置点之间的线。对我有用)

我这里还有一个示例文件:https://drive.google.com/file/d/1HkeJVgKeFeCuj2ItRn2s90ozy41zlCVL/view?usp=sharing

例如,如果您将.Border.LineStyle = xlLineStyleNone 行更改为.Border.LineStyle = xlContinuous 行,则输出如下:

如果你把它设置回.Border.LineStyle = xlLineStyleNone,那么你之间就没有线条了:

(请注意,我使用动态函数为点着色 - 您可以在此处查看 https://gist.github.com/Alex-ley/6fdaddda2b000072f70d98f90111a97e 和链接文件)

这里的所有 xlLineStyle:https://docs.microsoft.com/en-us/office/vba/api/excel.xllinestyle

【讨论】:

    猜你喜欢
    • 2021-08-16
    • 2012-10-25
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 2012-03-18
    • 2016-08-05
    相关资源
    最近更新 更多