Adding lines to worksheet using VBA...

hollifd

Board Regular
Joined
Apr 3, 2002
Messages
248
I am trying to add lines to sheet1 using VBA. I have not been able to figure out how to change the color and thickness of the lines. Can someone help?

I want to be able to add a line using VBA, give the line a Name so that I can refer to it in code and move it around to the exact position that I want. Ideally, I want to create a line with an arrow facing to the right. After creating the line, I want to adjust the top, left, width, name, color, line weight, etc. I cannot seem to get the syntax correct for adjusting all of these properties.

Here is the code that I am using...

<pre>
Dim MyName as string
Set myDocument = Worksheets(1)
Counter = Counter + 1
With myDocument.Shapes.AddLine(10, 50, 100, 50)
MyName = .Name
.Height = 0
End With
</pre>

Thanks for any help,
David
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If you add a line whilst recording a macro you should get the correct syntax.
 
Upvote 0
In 2007, it appears that the Macro recorder no longer records the actions associated with creating lines.

Anyone else have any code samples for me to create a simple line with an arrow and allow me to give the line a name, color, line thickness, top, left, width values?

Thanks,
David
 
Upvote 0
From the recorder in Excel 2010

Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 144.75, 99, 482.25, 232.5 _
        ).Select
        Selection.ShapeRange.Line.EndArrowheadStyle = msoArrowheadOpen
    With Selection.ShapeRange.Line
        .Visible = msoTrue
        .Weight = 2
    End With
    With Selection.ShapeRange.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 0)
        .Transparency = 0
    End With
End Sub
 
Upvote 0
Almost there...
How can I give it a name so that I can refer to it in code.

I tried

.Name and it does not like that.

Thanks,
David
 
Upvote 0
Nevermind...

I got it. This does it for me...

ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 144.75, 99, 482.25, 232.5).Select
Selection.ShapeRange.Line.EndArrowheadStyle = msoArrowheadOpen
Selection.Name = "MyShapeNameGoesHere"


Thanks for all of your help.
David
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top