VBA To Format Border Lines

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
245
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am really struggling to use VBA to automatically add borders - see below:

Data is always from column A to S but varies in length

Task 1 - Add an extra think border around the whole worksheet (Code is below)
So essentially I just need a way of selecting all the cells from A1 to the last populate cell in column A and then across to column S

Code:
Selection.Borders(xlDiagonalDown).LineStyle = xlNone    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThick
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

Task 2, In the same selected area as above I also want to add a dividing line when the data value changes from B2 through to the end of the document
So fundamentally if the were 3 rows with 841 in and then it changed to 842 I want a think line to be placed in between to separate them.

So assuming the code below would be part of it

Code:
With Selection.Borders(xlInsideVertical)        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

If anyone can help it would be greatly appreciated.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
For the first part try
Code:
Sub TkdKidSnake()
   With Range("A1").CurrentRegion
      .Borders.LineStyle = xlNone
      .BorderAround , xlThick
      .Borders(xlInsideVertical).LineStyle = xlThin
   End With
End Sub
 
Upvote 0
This worked an absolute treat Fluff thank you

For the first part try
Code:
Sub TkdKidSnake()
   With Range("A1").CurrentRegion
      .Borders.LineStyle = xlNone
      .BorderAround , xlThick
      .Borders(xlInsideVertical).LineStyle = xlThin
   End With
End Sub
 
Upvote 0
Ok, this should do part 2 as well
Code:
Sub TkdKidSnake()
   Dim Cl As Range
   Dim UsdCols As Long
   
   With Range("A1").CurrentRegion
      UsdCols = .Columns.Count
      .Borders.LineStyle = xlNone
      .BorderAround , xlThick
      .Borders(xlInsideVertical).Weight = xlThin
   End With
   For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
      If Cl.Value <> Cl.Offset(1).Value Then
         Cl.Offset(, -1).Resize(, UsdCols).Borders(xlEdgeBottom).Weight = xlThick
      End If
   Next Cl
End Sub
 
Upvote 0
Excellent Fluff many thanks works an absolute treat, I can stop tearing my hair out now :)

Ok, this should do part 2 as well
Code:
Sub TkdKidSnake()
   Dim Cl As Range
   Dim UsdCols As Long
   
   With Range("A1").CurrentRegion
      UsdCols = .Columns.Count
      .Borders.LineStyle = xlNone
      .BorderAround , xlThick
      .Borders(xlInsideVertical).Weight = xlThin
   End With
   For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
      If Cl.Value <> Cl.Offset(1).Value Then
         Cl.Offset(, -1).Resize(, UsdCols).Borders(xlEdgeBottom).Weight = xlThick
      End If
   Next Cl
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,375
Members
449,155
Latest member
ravioli44

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