【发布时间】:2018-07-17 22:37:49
【问题描述】:
我是使用 Excel 的 VBA 新手,并且一直在寻找有关此问题的答案几个小时但没有成功,因此希望有人可以帮助我。
我在 Excel 2016 中使用 VB 绘制了一个图表。这个图表有一个 XY 散点图和 2 条水平虚线来向查看者指示一个范围,但我希望在水平线之间画一个花括号来标记这个范围的含义.
这是我的代码示例:
Sub Create_Chart()
Dim co As ChartObject
Dim ct As Chart
Dim sc1 As SeriesCollection
Dim ser1 As Series
Dim intMaxX As Integer
Dim intMinX As Integer
Dim intMaxY As Integer
Dim intMinY As Integer
intMaxX = 20000
intMinX = 0
intMaxY = Range("Sheet1!C25").Value
intMinY = Range("Sheet1!C26").Value
Set co = Sheet2.ChartObjects.Add(Range("K1").Left, Range("K1").Top, 1000, 600)
Set ct = co.Chart
With ct
'Code for my main XYScatterplot chart is here. This all works fine so I am not including it
End With
'This code draws one of my horizontal dashed lines
With ct.SeriesCollection.NewSeries
.Name = ""
.XValues = Array(intMinX, intMaxX)
.Values = Array(intMinY, intMinY)
.Format.Line.ForeColor.RGB = RGB(255, 0, 0)
.Format.Line.DashStyle = msoLineDash
.MarkerStyle = xlMarkerStyleNone
End With
'This code draws my other horizontal dashed line
With ct.SeriesCollection.NewSeries
.Name = ""
.XValues = Array(intMinX, intMaxX)
.Values = Array(intMaxY, intMaxY)
.Format.Line.ForeColor.RGB = RGB(255, 0, 0)
.Format.Line.DashStyle = msoLineDash
.MarkerStyle = xlMarkerStyleNone
End With
'Then I tried to draw my curly brace. I want it to be placed between the two dashed lines
ct.Shapes.AddShape(msoShapeRightBrace, desired x-coordinate on my graph, intMaxY, 10, (intMaxY - intMinY)).TextFrame.Characters.Text = ""
End Sub
现在,这确实画了一个花括号,但它很小并且最终在右上角。它甚至似乎根本不属于我的图表。如何将它与我的图表关联,特别是与我的水平虚线的确切位置相关联,您可以看到它取决于我的一张工作表中的值。
非常感谢您的帮助。
更新: 感谢回复和我在其他地方读到的几篇文章,这就是我想出的:
Set curlyBrace = ct.Shapes.AddShape(msoShapeRightBrace, ct.PlotArea.InsideLeft, ct.PlotArea.InsideTop, 15, ct.PlotArea.InsideHeight)
curlyBrace.IncrementLeft ct.PlotArea.Left + ct.PlotArea.InsideWidth * 0.96
curlyBrace.IncrementTop ct.PlotArea.InsideHeight - ct.PlotArea.Position
With curlyBrace.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
End With
根据这段代码,我可以生成如下图(我只显示水平虚线和花括号)
现在如何使花括号的垂直起点和终点与两条水平线的位置相匹配?
非常感谢。
【问题讨论】: