Skip to main content

Border object

Table of contents
  1. Remarks
  2. Examples

Represents the border of an object.

Remarks

Most bordered objects (all except for the Range and Style objects) have a border that's treated as a single entity, regardless of how many sides it has. The entire border must be returned as a unit.

Use the Border property, such as from a TrendLine object, to return the Border object for this kind of object.

Note that the visual properties of a Border object are interlocked; that is, changing one property can induce changes in another. In most cases, the induced changes serve to make the border visible (which may or may not be desirable). However, other (more unexpected) results are possible.

Following is an example of interlocking with unexpected results. In this example, setting a border's Weight property to xlThick induces the LineStyle property to become xlSolid despite having previously set it to xlDashDotDot.

Sub InterlockingExample()
    Dim SomeRange As Range
    Dim SomeBorder As Border
    
    Set MyRange = Selection
    Set SomeBorder = MyRange.Borders(xlDiagonalDown)
    SomeBorder.Color = RGB(255, 0, 0)
    Debug.Print "SomeBorder.LineStyle = " & SomeBorder.LineStyle   'SomeBorder.LineStyle = 1
    Debug.Print "Set SomeBorder.LineStyle = xlDashDotDot"          'Set SomeBorder.LineStyle = xlDashDotDot
    SomeBorder.LineStyle = xlDashDotDot
    Debug.Print "SomeBorder.LineStyle = " & SomeBorder.LineStyle   'SomeBorder.LineStyle = 5
    Debug.Print "Set SomeBorder.Weight = xlThick"                  'Set SomeBorder.Weight = xlThick
    SomeBorder.Weight = xlThick
    Debug.Print "SomeBorder.LineStyle = " & SomeBorder.LineStyle   'SomeBorder.LineStyle = 1
End Sub

Examples

The following example changes the type and line style of a trend line on the active chart.

With ActiveChart.SeriesCollection(1).Trendlines(1)
    .Type = xlLinear
    .Border.LineStyle = xlDash
End With

Range and Style objects have four discrete borders—left, right, top, and bottom—which can be returned individually or as a group. Use the Borders property to return the Borders collection, which contains all four borders and treats the borders as a unit. The following example adds a double border to cell A1 on worksheet one.

Worksheets(1).Range("A1").Borders.LineStyle = xlDouble

Use Borders (index), where index identifies the border, to return a single Border object. The following example sets the color of the bottom border of cells A1:G1.

Worksheets("Sheet1").Range("A1:G1").Borders(xlEdgeBottom).Color = RGB(255, 0, 0)

Index can be one of the following XlBordersIndex constants: xlDiagonalDown, xlDiagonalUp, xlEdgeBottom, xlEdgeLeft, xlEdgeRight, xlEdgeTop, xlInsideHorizontal, or xlInsideVertical.

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>