如何通过VBA处理散点图中的标记?

时间:2019-12-19 09:10:57

标签: excel vba

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

这是我的宏:

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,并且不应用填充。

1 个答案:

答案 0 :(得分:0)

对我来说这可行:

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,则输出为:

enter image description here

如果将其重新设置为.Border.LineStyle = xlLineStyleNone,则您之间不会出现任何一行:

(请注意,我使用了动态功能为点着色-您可以在https://gist.github.com/Alex-ley/6fdaddda2b000072f70d98f90111a97e和链接文件中看到它)

此处所有xlLineStyles:https://docs.microsoft.com/en-us/office/vba/api/excel.xllinestyle

enter image description here