【问题标题】:Setting line on XY scatter without having a marker line在没有标记线的情况下在 XY 散点上设置线
【发布时间】:2017-08-01 04:33:27
【问题描述】:

所以我正在尝试使用 VBA 在 Excel 中格式化 XY 散点图,我希望用线条连接标记,但对于标记和标记线,我希望它们不可见。

由于某种原因,标记线和连接标记的线都使用相同的代码来更改其可见性属性。

chart.SeriesCollection(1).Format.Line.Visible = msoFalse

我可以使用MarkerBackGroundColor 属性独立更改颜色,但我似乎无法弄清楚如何在不使另一个可见的情况下使一个可见。

对此的任何帮助将不胜感激。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    你可以使用

    FullSeriesCollection(1).Border.LineStyle = xlNone
    

    FullSeriesCollection(1).Border.LineStyle = xlSolid
    

    仅格式化该行。

    【讨论】:

    • 感谢您的回复。不幸的是,我目前使用的是 Excel 2010,它不支持 FullSeriesCollection 对象。我相信我的办公室会在不久的将来升级到 Office 365,所以在那之前我可能只能勉强凑合。
    【解决方案2】:

    代码似乎设置了点。波纹管代码是设置点的示例。

    Sub ScatterChart_setPoint()
    
    Dim Ws As Worksheet
    Dim DB As Range, myCell As Range
    Dim Ch As Chart
    Dim i As Integer, n As Long, r As Integer, g As Integer, b As Integer
    Dim vX(), vY(), vLable(), vMarker
    Dim pnt As Point
    Dim Shp As Shape
    Dim h As Single, w As Single, l As Single, t As Single, fs As Single
    
    
        Application.DisplayAlerts = False
        Set Ws = ActiveSheet 'Sheets("Current Account")
        Ws.Activate
        Ws.Range("a65536").Select
        vMarker = Array(xlMarkerStyleCircle, xlMarkerStyleDash, xlMarkerStyleDiamond, xlMarkerStyleDot, _
                       xlMarkerStylePlus, xlMarkerStyleSquare, xlMarkerStyleStar, _
                       xlMarkerStyleTriangle, xlMarkerStyleX)
    
        Set DB = Ws.Range("h3", Ws.Range("h3").End(xlDown))  '<~~ range of data
        For Each myCell In DB
            If myCell = 0 Or myCell.Offset(, 10) = "" Then
            Else
                n = n + 1
                ReDim Preserve vX(1 To n)
                ReDim Preserve vY(1 To n)
                ReDim Preserve vLable(1 To n)
                vX(n) = myCell
                vY(n) = myCell.Offset(, 10)
                vLable(n) = myCell.Offset(, -7)
            End If
        Next myCell
    
        Charts.Add
    
        With ActiveChart
            .HasTitle = True
            .ChartType = xlXYScatter
            .Legend.Position = xlLegendPositionRight
            With .ChartTitle
                .Characters.Text = Ws.Range("a1").Value
                .Characters.Font.Size = 12
            End With
            .SeriesCollection.NewSeries
            With .SeriesCollection(1)
                .Name = "OECD"
                .XValues = vX
                .Values = vY
                .Trendlines.Add
                With .Trendlines(1)
                    .DisplayRSquared = True
                    .DisplayEquation = True
                End With
            End With
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = Ws.Range("r2")
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = Ws.Range("h2")
    
            For i = 1 To n
                Set pnt = .SeriesCollection(1).Points(i)
                With pnt
                    .ApplyDataLabels
                    .DataLabel.Text = vLable(i)
                    .DataLabel.ShowValue = False
                    .DataLabel.ShowCategoryName = True
                    .MarkerStyle = vMarker(WorksheetFunction.RandBetween(0, 8))
                    With WorksheetFunction
                        r = .RandBetween(0, 240)
                        g = .RandBetween(0, 240)
                        b = .RandBetween(0, 240)
                    End With
                    .MarkerForegroundColor = RGB(r, g, b)
                    .MarkerBackgroundColor = RGB(r, g, b)
                End With
                .ApplyDataLabels
            Next i
    
    
        Application.DisplayAlerts = True
    End Sub
    

    【讨论】:

    • 我看不出这个答案与原始问题有什么关系。在我看来,这只是完整代码的一些复制/粘贴,在没有任何解释的情况下做了不同的事情。
    • @Peh: FullSeriesCollection(1) 设置更改完整标记。它需要设置.SeriesCollection(1).Points(i)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    相关资源
    最近更新 更多