VBA Borders

Publication Date :

Blog Author :

Download FREE VBA Borders In Excel Template and Follow Along!
VBA Borders Excel Template.xlsm

Table Of Contents

arrow

Borders in VBA Excel

We use borders in Excel to highlight our data. In any dashboard, it is a very important skill to represent the important data, and borders do it. Borders are a property in VBA that we can access using the Range method and giving the appropriate border style as we know there are different border styles.

If you are good at formatting in excel, you can call yourself an "Excel Beautician." Formatting is vital in making the reports look appealing to the end-user or readers. We hope you are well aware of the formatting techniques in the regular worksheet. Formatting through VBA coding requires a considerable amount of VBA coding language. This article will show you one of the most underrated formatting techniques, i.e., applying Excel VBA borders.

VBA Borders
You are free to use this image on your website, templates, etc.. Please provide us with an attribution link.

Examples of Borders in VBA

Let us take a look at some examples to understand this in a better manner.

Example #1 - Apply VBA Borders with Coding

Creating a macro to apply Excel VBA borders with different styles, making them available as an add-on to the excel ribbon, makes the job easier whenever we want to apply VBA borders.

Every cell in a worksheet has borders and background colors. By default, every cell has no border and background color.

In the worksheet under the HOME tab, we have a border option. In addition, we have various options if you click on the drop-down list in excel.

VBA Borders Example 1

But in VBA, first, we must decide on the cell or range of cells. Then, we will apply the Excel VBA borders formatting styles. So, we can refer to the range of cells or cells using VBA RANGE object. So, for example, if you want to change the border of cell B5, then you can write the code like this.

Range(“B5”)

Then, we need to access the "Borders" property.

Code:

Sub Border_Example1()

  Range("B5").Bo

End Sub
VBA Borders Example 1-1

After applying the Excel VBA "Borders" property, we must open parenthesis to see all border formatting options..

Code:

Sub Border_Example1()

  Range("B5").Borders(

End Sub
VBA Borders Example 1-2

Here, we have xlDiagonalDown, xlDiagonalUp, xlEdgeBottom, xlEdgeLeft, xlEdgeRight, xlEdgeTop, xlInsideHorizontal, and xlInsideVertical.

VBA Borders Example 1-3

After selecting the border style, we need to select the characteristic we want to work with. One of the characters we need to use here is "LineStyle," so select the "LineStyle" property.

Code:

Sub Border_Example1()

  Range("B5").Borders (xlEdgeBottom).li

End Sub
VBA Borders Example 1-4

Once the line style property is selected, we need to set the line style type of line or type of borders we will apply in VBA.

Put an equal sign and select "XlLineStyle" enumeration.

Code:

Sub Border_Example1()

  Range("B5").Borders(xlEdgeBottom).LineStyle =XlLineStyle.

End Sub
VBA Borders Example 1-5

Put a dot to see all the available border styles.

Code:

VBA Borders Example 1-6

We have many options here. xlContinuous, xldash, xlDashDot, xlDashDotDot, xlDot, xlDouble, XlLineStyleNone, and xlSlantDashDot.

Now, we have selected the option of "xlDouble."

Code:

Sub Border_Example1()

  Range("B5").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlDouble

End Sub

Now, if I run the code, it will apply the Double line to the bottom of cell B5.

VBA Borders Example 1-7

Line Type: “xlContinuous”.

Code:

Sub Border_Example1()

  Range("B5").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous

End Sub

Result:

VBA Borders Example 1-8

Line Type: “clash.”

Code:

Sub Border_Example1()

  Range("B5").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlDash

End Sub

Result:

VBA Borders Example 1-9

Line Type: “xlDashDot.”

Code:

Sub Border_Example1()

  Range("B5").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlDashDot

End Sub

Result:

VBA Borders Example 1-10

Line Type: “xlDashDotDot”.

Code:

Sub Border_Example1()

  Range("B5").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlDashDotDot

End Sub

Result:

VBA Borders Example 1-11

Line Type: “xlDot.”

Code:

Sub Border_Example1()

  Range("B5").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlDot

End Sub

Result:

Example 1-12

Line Type: “xlLineStyleNone”.

Code:

Sub Border_Example1()

  Range("B5").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlLineStyleNone

End Sub

Result:

Example 1-13

It will remove the border of the specified cell.

Line Type: “xlSlantDashDot”.

Code:

Sub Border_Example1()

  Range("B5").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlSlantDashDot

End Sub

Result:

Example 1-14

Example #2 - Change the Border Using VBA Borders Around Method

We can also change the cell's borders using the VBA Borders Around method. Once we mention the range of cells or cells, we need to access the VBA Borders Around method.

Example 2

Open parenthesis to see all the parameters.

Range(“B5”).BorderAround(, , , , )

We can mention the line style, color, border weight, and many more things we can do with this method.

Code:

Sub Border_Example1()

  Range("B5").BorderAround LineStyle:=xlContinuous, Weight:=xlThick

End Sub

It will change the Line Style to xlContinuous.

LineStyle:=xlContinuous

The weight of the Border is thick.

Weight:=xlThick

And the result of this code is as below.

Example 2-1

Like this, using Excel VBA Borders and Border Around property and method, we can change the border and elements of borders through VBA coding.