VBA: Adding borders to a table

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
I am trying to add borders to a table. If you look at the picture below, I am wanting the inside of the table to be outlined with thin lines and the outside of the table to have a thick border. I recorded a macro doing this but it gave me an absurd amount of code lines. Is there a clean way to code borders?

Excel 2007
ABC
4MillSAP Item #SAP Agreement #
5Escanaba23423xxx
6Luke25235xcvs
7Wisconsin Rapids43sdssd

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Email Contents
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
actually only this

Code:
  With Selection.Borders         
    .LineStyle = xlContinuous         
    .Weight = xlThin         
    .ColorIndex = xlAutomatic     
 End With

but as you require 2 borders on selected table you might go with this

Code:
Dim myBorders() As Variant, myBorders2() As Variant, item As Variant, item2 As Variant
myBorders = Array(xlEdgeLeft, _
xlEdgeTop, _
xlEdgeBottom, _
xlEdgeRight, _
xlInsideVertical, _
xlInsideHorizontal)

myBorders2 = Array(xlEdgeLeft, _
xlEdgeTop, _
xlEdgeBottom, _
xlEdgeRight)

For Each item In myBorders
With Selection.Borders(item)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Next item

For Each item2 In myBorders2
With Selection.Borders(item2)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Next item2
 
Upvote 0

Forum statistics

Threads
1,203,107
Messages
6,053,556
Members
444,673
Latest member
Jagadeshrao

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top