vba 在Excel VBA中添加一条线,更改粗细并“向上”绘制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23532194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a Line in Excel VBA, Changing the thickness and drawing "up"
提问by Adam12344
I am trying to draw lines with Excel VBA and it is working nicely except for 2 things, 1) How do I make the line thicker? 2) The lines that go up and to the right do not get drawn. The ones that are horizontal or go down and to the right draw just fine.
我正在尝试用 Excel VBA 画线,除了两件事外,它工作得很好,1)如何使线条更粗?2)向上和向右的线没有被绘制。水平或向下并向右绘制的那些就好了。
Here is my code:
这是我的代码:
Sub DrawLine(FromCell As Range, ToCell As Range)
With FromCell.Parent.Shapes.AddLine(1, 1, 1, 1)
.Left = FromCell.Left + (FromCell.Width)
.Top = FromCell.Top + (FromCell.Height)
.Width = (ToCell.Left) - .Left
.Height = (ToCell.Top + (ToCell.Height)) - .Top
End With
End Sub
Sub GoDraw()
Call DrawLine(Range("D3"), Range("H3"))
Call DrawLine(Range("D3"), Range("H7"))
Call DrawLine(Range("D3"), Range("H11"))
Call DrawLine(Range("D7"), Range("H3"))
Call DrawLine(Range("D7"), Range("H7"))
Call DrawLine(Range("D7"), Range("H11"))
Call DrawLine(Range("D11"), Range("H3"))
Call DrawLine(Range("D11"), Range("H7"))
Call DrawLine(Range("D11"), Range("H11"))
End Sub
回答by CtrlDot
Here is an article I wrote a few years ago on programming shapes with VBA. See the "Adding Connectors and Lines" section.
这是我几年前写的一篇关于使用 VBA 编程形状的文章。请参阅“添加连接器和线路”部分。
http://www.breezetree.com/articles/excel-2007-autoshapes-vba.htm
http://www.breezetree.com/articles/excel-2007-autoshapes-vba.htm
The lines that go "up and to the right" are called elbow connectors, and you can set that by setting the MsoConnectorType variable when adding a line.
“向上和向右”的线称为弯头连接器,您可以通过在添加线时设置 MsoConnectorType 变量来设置它。