【问题标题】:Excel VBA - How do you set line style for chart series?Excel VBA - 如何为图表系列设置线条样式?
【发布时间】:2012-01-09 17:56:10
【问题描述】:

我希望图表中的绘图线根据用户指定的特定标准为实心、圆点或方点。我可以使用宏成功地为绘图设置线条颜色和标记样式,但我似乎找不到保存绘图线条样式属性值的对象。我尝试过使用录制宏功能,但是在属性窗口中更改线型并没有显示在代码中,并且运行录制的宏没有效果。

非常感谢任何帮助!

【问题讨论】:

    标签: charts excel vba


    【解决方案1】:
    YourChartSeries.Border.LineStyle = [some value from the XlLineStyle enumeration]
    

    更新:在 XL 2010 中录制我明白了 -

    ActiveChart.SeriesCollection(1).Select
    With Selection.Format.Line
        .Visible = msoTrue
        .DashStyle = msoLineSysDot
    End With
    ActiveChart.SeriesCollection(2).Select
    With Selection.Format.Line
        .Visible = msoTrue
        .DashStyle = msoLineSysDash
    End With
    

    这可能是您正在寻找的。​​p>

    【讨论】:

    • 我尝试在 excel 中手动设置线条样式并使用 MsgBox (CurrentChart.SeriesCollection(1).Border.LineStyle),无论线条是方点、圆点还是返回值 -4105虚线等。
    • 你的问题令人困惑——你问的是连接标记的线,还是标记本身?请使用您拥有的任何代码更新您的问题:这总是有助于获得有用的答案。
    • 如果您手动右键单击绘制的系列并单击“线条样式”下的“格式化数据系列...”,您可以更改虚线类型。我希望能够将程序中的数据系列行格式化为“圆点”和“方点”。但是 XLineStyle 枚举不提供这些破折号选项。从那以后,我决定放弃“方点”和“圆点”样式,而改用 XlContinuous、XlDot 和 XlDash。它不像方形和圆形圆点那么漂亮,但它可以完成工作。
    【解决方案2】:

    创建一个包含 255 个数据系列的图表,运行代码(并根据需要进行其他格式化)。然后将其保存为模板。

    Sub dd()
    
    Dim wb As Workbook
    Set wb = Application.ActiveWorkbook
    
    Dim myChart As Chart
    Set myChart = wb.Charts("Chart5")
    
    Dim mySeries As series
    
    For Each mySeries In myChart.SeriesCollection
        mySeries.Format.Line.Weight = 1#
    Next
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      如果你有一个折线图,我最终创建了一个 switch 语句,因为我不知道如何创建一个“名称”变量数组。见list of line types here

      For j = i To num_lines_to_plot
              count = count + 1
              ActiveChart.SeriesCollection.NewSeries
              With ActiveChart.SeriesCollection(j)
                  .Name = _
                      "='"**... (you'll need to fill this in)**
                  'create gradient
                  .Format.Line.ForeColor.RGB = RGB(255, 20 * count, 0)
                  'modify linetype
                  If count > 4 Then
                      line_type = count Mod 4
                  Else
                      line_type = count
                  End If
      
                  Select Case line_type
                      Case Is = 1
                          .Format.Line.DashStyle = msoLineSolid
                      Case Is = 2
                          .Format.Line.DashStyle = msoLineSquareDot
                      Case Is = 3
                          .Format.Line.DashStyle = msoLineDash
                      Case Is = 4
                          .Format.Line.DashStyle = msoLineLongDash
                      End Select
      
              End With
          Next
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-18
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多