Adam12344
Adam12344

Reputation: 1053

Adding a Line in Excel VBA, Changing the thickness and drawing "up"

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.

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

Upvotes: 0

Views: 7248

Answers (1)

CtrlDot
CtrlDot

Reputation: 3122

Here is an article I wrote a few years ago on programming shapes with VBA. See the "Adding Connectors and Lines" section.

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.

Upvotes: 1

Related Questions