You are reading the article How To Use Border In Excel Vba (Excel Template) updated in September 2023 on the website Happystarlongbien.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How To Use Border In Excel Vba (Excel Template)
Borders in Excel VBABorders 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 VBATo use borders in VBA we need to follow these steps as follows:
Use range method to access the range value.
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.
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 BordersLet 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.
Step 3: Declare a sub-function which means naming our macro.
Code:
Sub
Sample()End Sub
Step 4: Activate the worksheet first in order to use its properties by the following code below,
Code:
Sub
Sample() Worksheets("Sheet1").ActivateEnd Sub
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").BordersEnd Sub
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
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
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).LineStyleEnd Sub
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.xlDoubleEnd Sub
Step 10: Let us run the above code by pressing F5 and see the result in sheet 1 as follows,
Example #2 – VBA BorderNow 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
Step 2: Activate the worksheet by the following code written below,
Code:
Sub
Sample1() Worksheets("Sheet1").ActivateEnd Sub
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.xlDashDotEnd Sub
Step 4: Now run the above code and see the result in Sheet 1 as follows,
Example #3 – VBA BorderLet 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
Step 2: Activate the worksheet by the following code written below,
Code:
Sub
Sample2() Worksheets("Sheet3").ActivateEnd Sub
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.xlSlantDashDotEnd Sub
Example #4 – VBA BorderNow 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,
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
Step 2: Activate the worksheet by the following code written below,
Code:
Sub
Sample3() Worksheets("Sheet2").ActivateEnd Sub
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").BorderAroundEnd Sub
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:=xlThickEnd Sub
Step 5: Run the above code by pressing F5 and see the result in sheet 2 as follows,
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 ArticlesThis 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 –
You're reading How To Use Border In Excel Vba (Excel Template)
Update the detailed information about How To Use Border In Excel Vba (Excel Template) on the Happystarlongbien.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!