VBA:为散点图中的误差线创建不同的格式

标签 vba excel charts

我正在尝试使用 vba 创建项目时间表。我想为垂直(Y)和水平(X)误差线创建不同的颜色/样式格式。我首先使用 Record Macro 来查看 X 和 Y 条的选择方式如何不同,但两者都显示为相同的 ErrorBars 属性。我希望 Y 误差线用虚线显示,但是当我添加那行代码时,X 和 Y 误差线都会改变。有什么建议么?

Sub Timeline()

Dim sheet1 As Worksheet 
Dim timeline As Chart

Set sheet1 = ActiveWorkbook.Worksheets("sheet1") 
Set timeline = sheet1.Shapes.AddChart.Chart

With timeline

  .ChartType = xlXYScatter
  .SeriesCollection.NewSeries 
  .SeriesCollection(1).Name ="Project Lengths" 
  .SeriesCollection(1).XValues ="='sheet1'!C7:C25" 
  .SeriesCollection(1).Values ="='sheet1'!D7:D25" 

  .SeriesCollection(1).ErrorBar Direction:=xlX, Include:= _ 
      xlPlusValues, Type:=xlCustom, Amount:="='sheet1'!E7:E25"
  .SeriesCollection(1).ErrorBars.EndStyle = xlNoCap 
  .SeriesCollection(1).ErrorBars.Format.Line.Visible = msoTrue  
  .SeriesCollection(1).ErrorBars.Format.Line.ForeColor.RGB = RGB(0, 112, 192) 
  .SeriesCollection(1).ErrorBars.Format.Line.Transparency = 0 
  .SeriesCollection(1).ErrorBars.Format.Line.Weight = 2.5 

  .SeriesCollection(1).ErrorBar Direction:=xlY, Include:= _ 
      xlMinusValues, Type:=xlPercent, Amount:=100
  .SeriesCollection(1).ErrorBars.EndStyle = xlNoCap 
  .SeriesCollection(1).ErrorBars.Format.Line.Visible = msoTrue 
  .SeriesCollection(1).ErrorBars.Format.Line.DashStyle = msoLineSysDash 

End With

End Sub

最佳答案

您使用的是哪个版本的 Excel?
我刚刚在 Excel 2013 中尝试了您的代码,它似乎可以工作。

enter image description here

水平误差条为蓝色虚线,垂直误差条为黑色且完整。这有点令人困惑,因为它看起来像一个带有非常细的条形和水平误差线的条形图,但它可以工作(至少在 Excel 2013 中)。

关于VBA:为散点图中的误差线创建不同的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23836138/

相关文章:

javascript - 在条形图顶部显示计数 - ChartJS

charts - Google 条形图分组和堆叠

javascript - D3 : Create a grouped bar chart from json objects

vba - 使用 VBA 访问 Outlook 中的文件夹

excel - 使用 Excel VBA 将 Skype 消息发送到群聊

VBA:是否可以在一行 "Nothing"中声明和实例化对象?

excel - 在 Excel 中穿插不同列的单元格

c# - 导出到excel打开它

excel - 在 VBA 中,使用 With、End With 是否总是更好?

vba - 在 Excel 2010 上寻找更好定义的范围?