Skip to main content

Formatting Cells Border with VBA

Border Index

Use Borders (index), where index identifies the border, to return a single Border object.

Index can be one of the following XlBordersIndex constants:

Name Value Description
xlDiagonalDown 5 Border running from the upper-left corner to the lower-right of each cell in the range.
xlDiagonalUp 6 Border running from the lower-left corner to the upper-right of each cell in the range.
xlEdgeBottom 9 Border at the bottom of the range.
xlEdgeLeft 7 Border at the left edge of the range.
xlEdgeRight 10 Border at the right edge of the range.
xlEdgeTop 8 Border at the top of the range.
xlInsideHorizontal 12 Horizontal borders for all cells in the range except borders on the outside of the range.
xlInsideVertical 11 Vertical borders for all the cells in the range except borders on the outside of the range.

The following example sets the color of the bottom border of cells A1:D1.

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

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 range B2:E5 on worksheet one.

Worksheets(1).Range("B2:E5").Borders.LineStyle = xlDouble

Line Style

The value of this property can be set to one of the XlLineStyle constants:

Name Value Description
xlContinuous 1 Continuous line.
xlDash -4115 Dashed line.
xlDashDot 4 Alternating dashes and dots.
xlDashDotDot 5 Dash followed by two dots.
xlDot -4142 Dotted line.
xlDouble -4119 Double line.
xlLineStyleNone -4118 No line.
xlSlantDashDot 13 Slanted dashes.

The following code example sets the border on the bottom edge of cell A1 with continuous line.

Range("A1").Borders(xlEdgeBottom).LineStyle = xlContinuous

The following code example removes the border on the bottom edge of cell A1.

Range("A1").Borders(xlEdgeBottom).LineStyle = xlNone

Line Thickness

The value of Borders Weight property property can be set to one of the XlBorderWeight constants

Name Value Description
xlHairline 1 Hairline (thinnest border).
xlMedium -4138 Medium.
xlThick 4 Thick (widest border).
xlThin 2 Thin.

The following code example sets the thickness of the border created to xlThin (Thin).

Range("A1").Borders(xlEdgeBottom).Weight = xlThin

Line Color

The value of Borders Color property can be set to:

  1. Color constants: vbBlack, vbRed, vbGreen, vbYellow, vbBlue, vbMagenta, vbCyan, vbWhite.
  2. An integer value from 0 to 16,581,375 (RGB color value, 255 * 255 * 255).
  3. RGB function.
  4. XlRgbColor.

The following code example sets the color of the border on the bottom edge to green.

Range("A1").Borders(xlEdgeBottom).Color = vbGreen

The following example sets the color of the bottom border of cell A1 with RGB fuction.

Range("A1").Borders(xlEdgeBottom).Color = RGB(255, 0, 0)

The following example sets the color of the bottom border of cell A1 to 0 (Black).

Range("A1").Borders(xlEdgeBottom).Color = 0

The following example sets the color of the borders of cell A1 to rgbBlue (Blue).

Range("A1").Borders.Color = rgbBlue

The following example uses Borders ColorIndex to set the borders color of the range B2:G3 to 3.

Worksheets("Sheet1").Range("B2:G3").Borders.ColorIndex = 3

The following example uses Borders ThemeColor to set the borders color of the range B2:G3 to 8.

Worksheets("Sheet1").Range("B2:G3").Borders.ThemeColor = 8

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>