condensed border code

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
697
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

How could I go about shortening the below code? Thank you
VBA Code:
    Range("B2:D" & Range("A" & Rows.Count).End(xlUp).Row).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With

    Range("E2:G" & Range("A" & Rows.Count).End(xlUp).Row).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With

    Range("H2:J" & Range("A" & Rows.Count).End(xlUp).Row).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With

    Range("K2:M" & Range("A" & Rows.Count).End(xlUp).Row).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With

Basically, I want to apply -
VBA Code:
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlThick
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThick
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThick
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
To the following -
VBA Code:
        Range("B2:D" & Range("A" & Rows.Count).End(xlUp).Row).Select
        Range("E2:G" & Range("A" & Rows.Count).End(xlUp).Row).Select
        Range("H2:J" & Range("A" & Rows.Count).End(xlUp).Row).Select
        Range("K2:M" & Range("A" & Rows.Count).End(xlUp).Row).Select
[/CODE]
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
79,761
Office Version
  1. 365
Platform
  1. Windows
You can slim it down slightly like
VBA Code:
   With Range("B2:D" & Range("A" & Rows.Count).End(xlUp).Row)
      .Borders.Weight = xlThin
      .Borders(xlEdgeLeft).Weight = xlThick
      .Borders(xlEdgeTop).Weight = xlThick
      .Borders(xlEdgeRight).Weight = xlThick
   End With
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
79,761
Office Version
  1. 365
Platform
  1. Windows
You can also do it like
VBA Code:
   With Range("B2:M" & Range("A" & Rows.Count).End(xlUp).Row)
      .Borders.Weight = xlThin
      .Borders(xlEdgeLeft).Weight = xlThick
      .Columns(4).Borders(xlEdgeLeft).Weight = xlThick
      .Columns(7).Borders(xlEdgeLeft).Weight = xlThick
      .Columns(10).Borders(xlEdgeLeft).Weight = xlThick
      .Borders(xlEdgeTop).Weight = xlThick
      .Borders(xlEdgeRight).Weight = xlThick
   End With
This replaces all your posted code.
 
Solution

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
697
Office Version
  1. 2016
Platform
  1. Windows
You can also do it like
VBA Code:
   With Range("B2:M" & Range("A" & Rows.Count).End(xlUp).Row)
      .Borders.Weight = xlThin
      .Borders(xlEdgeLeft).Weight = xlThick
      .Columns(4).Borders(xlEdgeLeft).Weight = xlThick
      .Columns(7).Borders(xlEdgeLeft).Weight = xlThick
      .Columns(10).Borders(xlEdgeLeft).Weight = xlThick
      .Borders(xlEdgeTop).Weight = xlThick
      .Borders(xlEdgeRight).Weight = xlThick
   End With
This replaces all your posted code.
That is a great tip, adding in the specific column by number. Thank you!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
79,761
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,181,679
Messages
5,931,370
Members
436,788
Latest member
er19

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
Top