EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Tips VBA Borders
 

VBA Borders

Madhuri Thakur
Article byMadhuri Thakur

Excel VBA Border

Borders in Excel VBA

Borders are a necessary part of every worksheet or in any word file. Borders separate data from one another it shows which part of data is referred to which area to avoid any confusion. Also, it looks good to have borders in our datasheet. In excel worksheet we have options for inserting borders manually, but how we do it in VBA is what we will learn in this article.

 

 

To apply borders in VBA we need to access the properties of a cell and in the current case, the property we need to access is borders. Now we need to keep in mind that there are various types of options available in borders properties. Such as diagonal vertical up down etc. We will cover some of them here.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How to Use Borders in VBA

To use borders in VBA we need to follow these steps as follows:

  1. Use range method to access the range value.
  2. Use borders method if we want to format only a portion of cell or borders around if we want to cover the cells with borders.
  3. Use different line styles to make the borders look pleasant.
You can download this VBA Border Excel Template here – VBA Border Excel Template

Now let us go through some examples and see how we can insert a border in excel cell.

Example #1 – VBA Borders

Let us use the basic enumerations what VBA provides us when we type the keywords to see the borders. We will put borders in cell A1 and see the result.

Step 1: Go to Developer’s tab, open visual basic and we will see a VB Editor.

Developer's Tab

Step 2: Insert a new module from the insert tab provided. Click on the module we just inserted which will open a code window for us,

Module

Step 3: Declare a sub-function which means naming our macro.

Code:

Sub Sample()

End Sub

Sub Function

Step 4: Activate the worksheet first in order to use its properties by the following code below,

Code:

Sub Sample()

Worksheets("Sheet1").Activate

End Sub

VBA Border Example 1.2

Step 5: Now let us try to change the border of cell A1. Use the range method as below,

Code:

Sub Sample()

Worksheets("Sheet1").Activate
Range("A1").Borders

End Sub

VBA Border Example 1.3

Step 6: Select the borders properties which will give us an option to select the border style as follows,

Code:

Sub Sample()

Worksheets("Sheet1").Activate
Range("A1").Borders(

End Sub

Border Example 1.5

Step 7: Select the first option which is Xdiagonalup as the border style.

Code:

Sub Sample()

Worksheets("Sheet1").Activate
Range("A1").Borders (xlDiagonalUp)

End Sub

Bdr Example 1.6

Step 8: Now we need to use the line style for borders. After dot(.) operator use enumerations for line style as follows,

Code:

Sub Sample()

Worksheets("Sheet1").Activate
Range("A1").Borders(xlDiagonalUp).LineStyle

End Sub

Bdr Example 1.7

Step 9: Type = sign and it will give us the numerous enumerations for linestyle as follows,

Code:

Sub Sample()

Worksheets("Sheet1").Activate
Range("A1").Borders(xlDiagonalUp).LineStyle = XlLineStyle.xlDouble

End Sub

Bdr Example 1.8

Step 10: Let us run the above code by pressing F5 and see the result in sheet 1 as follows,

VBA Border 1

Example #2 – VBA Border

Now let us use the other method for the border style in VBA.

Step 1: We already have our module inserted, Name a macro in it with another sub-function as follows,

Code:

Sub Sample1()

End Sub

VBA Border Example 2.1

Step 2: Activate the worksheet by the following code written below,

Code:

Sub Sample1()

Worksheets("Sheet1").Activate

End Sub

Bdr Example 2.2

Step 3: Now let use the range method to activate the border properties such as shown below,

Code:

Sub Sample1()

Worksheets("Sheet1").Activate
Range("C1").Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlDashDot

End Sub

Bdr Example 2.3

Step 4: Now run the above code and see the result in Sheet 1 as follows,

VBA border 2

Example #3 – VBA Border

Let us try a few more of the border and line styles in another cell. This time we will use it in a cell range C5:E6.

Step 1: We already have our module inserted, Name a macro in it with another sub-function as follows,

Code:

Sub Sample2()

End Sub

VBA Border Example 3.1

Step 2: Activate the worksheet by the following code written below,

Code:

Sub Sample2()

Worksheets("Sheet3").Activate

End Sub

VBA Border Example 3.2

Step 3: Now let use the range method to activate the border properties such as shown below,

Code:

Sub Sample2()

Worksheets("Sheet3").Activate
Range("C5:E6").Borders(xlEdgeTop).LineStyle = XlLineStyle.xlSlantDashDot

End Sub

Bdr Example 3.3

Step 4: Now run the above code and see the result in Sheet 1 as follows,

VBA Border 3

Example #4 – VBA Border

Now in this example, we will use borders around the cell covering the whole cell. Earlier what we did was border only one portion of the cell. Consider the following data we have in sheet 2 as follows,

VBA Border Example 4.1

Let us try to use a border around this data using the border around the method.

Step 1: We already have our module inserted, Name a macro in it with another sub-function as follows,

Code:

Sub Sample3()

End Sub

VBA Border Example 4.2

Step 2: Activate the worksheet by the following code written below,

Code:

Sub Sample3()

Worksheets("Sheet2").Activate

End Sub

Bdr Example 4.3

Step 3: Now let’s use the range method to activate the border around properties such as shown below,

Code:

Sub Sample3()

Worksheets("Sheet2").Activate
Range("A1:B6").BorderAround

End Sub

Bdr Example 4.4

Step 4: Now use the line style and line thickness as follows,

Code:

Sub Sample3()

Worksheets("Sheet2").Activate
Range("A1:B6").BorderAround LineStyle:=xlContinuous, Weight:=xlThick

End Sub

Bdr Example 4.5

Step 5: Run the above code by pressing F5 and see the result in sheet 2 as follows,

VBA Border 4

Things to Remember

  • Border around is used to cover all parts of cells.
  • Borders method is used to cover only a portion of a cell.
  • X Linestyles are used to use different types of styles in borders.
  • Inserting borders is similar to formatting data.
  • Borders in VBA are similar to borders in the worksheet, we need to remember the keyword for it to use.

Recommended Articles

This is a guide to VBA Borders. Here we discuss how to use Borders in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA IsNumeric
  2. VBA UCASE
  3. VBA Worksheets
  4. VBA RGB

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA Border Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW