Finance

Charts

Statistics

Macros

Search

Line In Excel VBA

The AddLine() method creates a line shape. The return value is an object of type Shape. For a line, the Line property of the Shape object controls the properties of the line itself. For larger shapes (such as rectangles), this property controls the border line characteristics.

Data arrays can contain not only variables but also references to objects. In this example, an array of references to three similar line shapes is created:

Sub CreateLines()
    Dim Sh(1 To 3) As Shape
    Dim i As Integer
    ThisWorkbook.Worksheets("Sheet3").Activate
    For i = 1 To 3
        Set Sh(i) = ActiveSheet.Shapes.AddLine(240, 30, 280, 30)
        Sh(i).Line.ForeColor.RGB = RGB(255, 0, 0)
        Sh(i).Line.Weight = 3
        Sh(i).Rotation = (i - 1) * 30
        Set Sh(i) = Nothing
    Next i
End Sub

Explanation:
The AddLine() method requires four parameters:

  • BeginX and BeginY specify the coordinates of the line’s starting point.
  • EndX and EndY specify the coordinates of the line’s ending point.

Inside the loop, the color and thickness of each line are set uniformly.

The Rotation property is set differently for each line, increasing in increments of 30 degrees for each successive line.

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx