How to add cell borders in VBA

Hello friends! Today we’ll be learning how to format cells with borders using Excel VBA. Very simple and easy to follow tutorial with reusable code.

Below the code the border the cells individually and outside only. There are other properties to color, thickness and other which we’ll discuss in next tutorial

Sub border()
'----Individual cells borders (from cell B2 to E11)
Worksheets("Sheet1").Range("B2:E11").Borders(xlEdgeTop).LineStyle = xlContinuous
Worksheets("Sheet1").Range("B2:E11").Borders(xlEdgeBottom).LineStyle = xlContinuous
Worksheets("Sheet1").Range("B2:E11").Borders(xlEdgeLeft).LineStyle = xlContinuous
Worksheets("Sheet1").Range("B2:E11").Borders(xlEdgeRight).LineStyle = xlContinuous
Worksheets("Sheet1").Range("B2:E11").Borders(xlInsideVertical).LineStyle = xlContinuous
Worksheets("Sheet1").Range("B2:E11").Borders(xlInsideHorizontal).LineStyle = xlContinuous
'----Outside borders
Worksheets("Sheet1").Range("K2:M11").BorderAround LineStyle:=xlContinuous
End Sub

Few of the line types available in VBA

'---- available Line styles
' Continuous - xlContinuous
' Dot - xlDot
' DashDotDot - xlDashDotDot
' Dash - xlDash
' SlantDashDot - xlSlantDashDot
' Double - xlDouble

Keep visiting Analytics Tuts for more tutorials.

Thanks for reading! Comment your suggestions and queries


Leave a Reply

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