30 Advanced Excel Tips for 2026

Excel Champs / VBA / How to Apply Borders on a Cell using VBA in Excel
By Puneet Gogia (Microsoft MVP)

How to Apply Borders on a Cell using VBA in Excel

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Range("A1:D10").Borders.LineStyle = xlContinuous puts a thin border on every edge of every cell in the range, like All Borders on the Home tab. For one edge, name it: Range("A1:D1").Borders(xlEdgeBottom).LineStyle = xlContinuous.

The edges are xlEdgeTop, xlEdgeBottom, xlEdgeLeft and xlEdgeRight around the outside, xlInsideHorizontal and xlInsideVertical between the cells, and xlDiagonalUp and xlDiagonalDown across each cell. Set .Weight to xlThin, xlMedium or xlThick, and .Color to a colour such as RGB(0, 0, 0).

To draw only an outline around the whole block, use Range("A1:D10").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium. All its arguments are optional.

Range("A1:D10").Borders.LineStyle = xlNone removes the edge and inside borders. Diagonal lines are separate, so clear them with .Borders(xlDiagonalUp).LineStyle = xlNone and the same for xlDiagonalDown. A border on a neighbouring cell can still show along a shared edge.

To apply borders to a cell using VBA in Excel, you can use two different ways. The first is the “Borders” property and the second is the “BorderAround” method. The main difference between these two ways is that with the “Borders” property, you can apply the border to just one side of the cell, while BorderAround applies the border around the whole cell or range.

In this tutorial, we will explore both ways and look at the different ways to add a border.

VBA Borders Property

  1. First, you need to specify the range or the cell where you wish to apply the border using the range object.
    specify the range or cell
  2. After that, type a dot (.) and then select the “Borders” property from the list of properties and methods.
    type a dot and then select the border
  3. Next, specify the border index from the constants available. Here I’m using “xlEdgeBottom” to apply border only to the bottom of the cell.
    specify the border index
  4. From here, specify the line style using the “LineStyle” property. I’m using “xlContinuous” as the line style.
    specify th line style
Sub vba_borders()

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

End Sub

Now when you run this code, it will add a border to the bottom of cell A1.

Apart from xlEdgeBottom, you can use xlEdgeTop, xlEdgeLeft and xlEdgeRight for the other sides of a cell, and for a range of cells, xlInsideHorizontal and xlInsideVertical for the borders between the cells.

it will add border to the bottom

Using Different Colors with Color Index/Color

Just like you choose the color for the border when you do it manually, with the Borders property, you can use the ColorIndex and Color properties to use a color other than the default color.

Consider the below code.

With Range("A1").Borders(xlEdgeBottom)

            .LineStyle = XlLineStyle.xlContinuous
            .Color = RGB(255, 0, 0)

End With

When you run this macro, it adds a border to the bottom of the cell with a red color.

using different colors with color index

Or if you want to use the ColorIndex property, then the code would be like this (7 is magenta in the default color palette):

With Range("A1").Borders(xlEdgeBottom)

            .LineStyle = XlLineStyle.xlContinuous
            .ColorIndex = 7

End With
color index property

Related: VBA With Statement

Add Border Inside a Cell

If you want to apply a border inside a cell, in that case, you need to use “xlDiagonalUp” and “xlDiagonalDown” as the xlBorderIndex.

With Range("A1").Borders(xlDiagonalUp)

            .LineStyle = XlLineStyle.xlDashDotDot
            .Color = RGB(255, 0, 0)

End With

With Range("A1").Borders(xlDiagonalDown)

            .LineStyle = XlLineStyle.xlDashDotDot
            .Color = RGB(255, 0, 0)

End With
add border inside a cell

When you run the above code, it adds diagonal borders inside the cell, both upward and downward.

VBA BorderAround Method

In VBA’s BorderAround method, you have 5 arguments (LineStyle, Weight, ColorIndex, Color and ThemeColor), and all of them are optional. Microsoft’s documentation says to specify either LineStyle or Weight (not both) and only one of the three color arguments. If you skip them, Excel uses the default line style and weight.

vba borderaround method

The following code applies the border around the range A1:A3 when you run the macro.

Range("A1:A3").BorderAround _
                LineStyle:=xlContinuous, _
                Weight:=xlThin

And here’s Microsoft’s own example, which adds a thick red border around A1:D4 on Sheet1 using only Weight and ColorIndex:

Sub thick_red_border_around()
Worksheets("Sheet1").Range("A1:D4").BorderAround ColorIndex:=3, Weight:=xlThick
End Sub
specify to apply border to a cell in the right way

Add Border to Each Cell in the Range

The following code loops through all the cells in the range and applies the border to each cell one by one.

Sub vba_borders()

Dim iRange As Range
Dim iCells As Range

Set iRange = Range("A1:A10")

For Each iCells In iRange
    iCells.BorderAround _
            LineStyle:=xlContinuous, _
            Weight:=xlThin
Next iCells

End Sub
Add Border to Each Cell in the Range

Apply Border to All the Cells with Text

The following code uses a FOR LOOP, USED RANGE, ISEMPTY, and IF STATEMENT to apply borders only to the cells where you have a value. Like the other short codes on this page, put these lines between Sub and End Sub in a module to run them.

Dim iRange As Range
Dim iCells As Range

Set iRange = ThisWorkbook.ActiveSheet.UsedRange

For Each iCells In iRange
    If Not IsEmpty(iCells) Then
    iCells.BorderAround _
            LineStyle:=xlContinuous, _
            Weight:=xlThin
    End If
Next iCells

Remove Borders

You just need to use the line style “xlNone”. The following line removes the diagonal-down border from cell A1:

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

And if you want to remove the border from all the cells in a worksheet where you have a value, consider the following code.

Dim iRange As Range
Dim iCells As Range

Set iRange = ThisWorkbook.ActiveSheet.UsedRange

For Each iCells In iRange
    If Not IsEmpty(iCells) Then
        iCells.Borders.LineStyle = xlNone
    End If
Next iCells

When you use Borders without an index, like iCells.Borders.LineStyle = xlNone, it removes all the borders of the cell at once. And to remove all the borders from the whole worksheet, use Cells.Borders.LineStyle = xlNone.

Apply All Borders to a Range

You don’t need a loop to add a border to every cell in a range. When you use the Borders collection without an index, VBA applies the border to all the edges and the inside lines of the range, just like the All Borders option on the Home tab:

Sub all_borders()
With Range("A1:C10").Borders
.LineStyle = xlContinuous
.Weight = xlThin
.Color = RGB(0, 0, 0)
End With
End Sub

Change the Border Weight

Use the Weight property to make the border thinner or thicker. You have four options: xlHairline, xlThin, xlMedium and xlThick. The following code adds a thick bottom border to A1:C1, which is handy for a header row:

Sub thick_bottom_border()
With Range("A1:C1").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThick
End With
End Sub

Frequently Asked Questions

What is the difference between Borders and BorderAround?

BorderAround only draws the outline of the whole range. Borders lets you pick each side, the inside lines and the diagonals, or all of them at once when you use it without an index.

How do I add borders to a range on another sheet?

Refer to the sheet before the range, like Worksheets(“Sheet2”).Range(“A1:C10”).Borders.LineStyle = xlContinuous. You don’t need to activate that sheet.

Can I use a theme color for a border?

Yes. Use the ThemeColor property of the border (or the ThemeColor argument of BorderAround), like .ThemeColor = xlThemeColorAccent1. Then the border color changes when you change the workbook theme.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel