VBA Code to Add Border To Excel Range

Complete Excel VBA Course

Through formatting the cells, you can make your Excel data more beautiful and easier to understand. If you record a macro to add borders to Excel range, you will notice that it generates 30 plus lines of code for just a small work.

VBA Code to Add Border to Excel Range

See below such code:
Sub Macro1()
    
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Range("E4").Select
End Sub

Here we are sharing one line of code which does the same thing:
Sheet1.Range("A1:G20").Borders.LineStyle = xlContinuous

Screenshot of the sheet before running the code:
VBA Code to Add Border to Excel Range

Screenshot of the sheet after running the code:
VBA Code to Add Border to Excel Range

How can I use this VBA code?


If you want to use this code in your VBA tool, then follow below steps:

  1. Open the Excel file in which you want to copy this code

  2. Press Alt+F11

  3. Open the module in which you want to add this code

  4. Paste this code just after the code where you want to add the borders in the sheet

VBA Code to Add Border to Excel Range

What will happen when I run this code?
  1. The code will add borders in range A1 to G20 of Sheet1

Thanks for reading the article, subscribe us to get more VBA tricks

Complete Excel VBA Course

Download Practice File

You can also practice this through our practice files. Click on the below link to download the practice file.

Recommended Articles

Excel VBA Course : Beginners to Advanced

We are offering Excel VBA Course for Beginners to Experts at discounted prices. The courses includes On Demand Videos, Practice Assignments, Q&A Support from our Experts. Also after successfully completion of the certification, will share the success with Certificate of Completion

This course is going to help you to excel your skills in Excel VBA with our real time case studies.

Lets get connected and start learning now. Click here to Enroll.

Secrets of Excel Data Visualization: Beginners to Advanced Course

Here is another best rated Excel Charts and Graph Course from ExcelSirJi. This courses also includes On Demand Videos, Practice Assignments, Q&A Support from our Experts.

This Course will enable you to become Excel Data Visualization Expert as it consists many charts preparation method which you will not find over the internet.

So Enroll now to become expert in Excel Data Visualization. Click here to Enroll.

Similar Posts

Leave a Reply

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