VBA Adjustment: Apply Border to Group of Cells

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
330
Office Version
  1. 365
Platform
  1. Windows
I am using the below VBA code to apply a border around cells. Right now it is only applying the border to columns A-J. What do I need to adjust to make the border expand to column K?

VBA Code:
Sub BorderIt()
    Dim Cel As Range, rng As Range, r As Long, a As Variant
    For r = 1 To Range("A" & Rows.Count).End(xlUp).Row
        Set Cel = Cells(r, 1)
        If Cel.MergeCells Then
            Set rng = Cel.MergeArea
            r = Split(Cel.MergeArea.Address, "$")(4) 
        Else
            Set rng = Cel
        End If
        For Each a In Array(rng, rng.Resize(, 10))
            a.BorderAround , Weight:=xlMedium
        Next a
    Next r
 
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try changing number 10 into 11:
Rich (BB code):
Sub BorderIt()
    Dim Cel As Range, rng As Range, r As Long, a As Variant
    For r = 1 To Range("A" & Rows.Count).End(xlUp).Row
        Set Cel = Cells(r, 1)
        If Cel.MergeCells Then
            Set rng = Cel.MergeArea
            r = Split(Cel.MergeArea.Address, "$")(4)        'get last row in merged cell
        Else
            Set rng = Cel
        End If
        For Each a In Array(rng, rng.Resize(, 11))
            a.BorderAround , Weight:=xlMedium
        Next a
    Next r
  
End Sub
 
Upvote 0
Solution
Think this should work - test on a COPY FIRST!

VBA Code:
Sub BorderIt()
    Dim Cel As Range, rng As Range, r As Long, a As Variant
    For r = 1 To Range("A" & Rows.Count).End(xlUp).Row
        Set Cel = Cells(r, 1)
        If Cel.MergeCells Then
            Set rng = Cel.MergeArea
            r = Split(Cel.MergeArea.Address, "$")(4) 
        Else
            Set rng = Cel
        End If
        For Each a In Array(rng, rng.Resize(, 11))
            a.BorderAround , Weight:=xlMedium
        Next a
    Next r
 
End Sub
 
Upvote 0
Try changing number 10 into 11:
Rich (BB code):
Sub BorderIt()
    Dim Cel As Range, rng As Range, r As Long, a As Variant
    For r = 1 To Range("A" & Rows.Count).End(xlUp).Row
        Set Cel = Cells(r, 1)
        If Cel.MergeCells Then
            Set rng = Cel.MergeArea
            r = Split(Cel.MergeArea.Address, "$")(4)        'get last row in merged cell
        Else
            Set rng = Cel
        End If
        For Each a In Array(rng, rng.Resize(, 11))
            a.BorderAround , Weight:=xlMedium
        Next a
    Next r
 
End Sub

Think this should work - test on a COPY FIRST!

VBA Code:
Sub BorderIt()
    Dim Cel As Range, rng As Range, r As Long, a As Variant
    For r = 1 To Range("A" & Rows.Count).End(xlUp).Row
        Set Cel = Cells(r, 1)
        If Cel.MergeCells Then
            Set rng = Cel.MergeArea
            r = Split(Cel.MergeArea.Address, "$")(4)
        Else
            Set rng = Cel
        End If
        For Each a In Array(rng, rng.Resize(, 11))
            a.BorderAround , Weight:=xlMedium
        Next a
    Next r
 
End Sub

Worked perfectly. Thank you.
 
Upvote 0
Glad we could help and thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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