Dynamic Range Borders – VBA code assistance

bootj1234

Board Regular
Joined
Aug 27, 2012
Messages
85
Following is some VBA code I am wrestling with in trying to create borders that stay within in a dynamic range.

As you can see from the following VBA code and image, I am able to get the code I have written to be dynamic in responding to finding the overall range of the data I want to enclose in borders, except for the thicker vertical borders. These thicker vertical borders are extending below the dynamic range, with row 19, as you can see in the image, constituting the last row in the range in this particular instance.

I would appreciate assistance with making the necessary adjustments in my code to get the thicker vertical borders to work within the dynamic range like the thinner borders do.

Thanks for your assistance!

-John

Sub DynamicRangeBorders()
Dim StartCell As Range
Set StartCell = Range("A11")
Cells.Borders.LineStyle = xlNone
StartCell.CurrentRegion.Borders.LineStyle = xlContinuous
Dim BorderIndex As Variant
For Each BorderIndex In Array(xlEdgeTop, xlEdgeLeft, xlEdgeRight)
With Range("L11:R35").Borders(BorderIndex)
.Weight = xlThick
.ColorIndex = vbBlack
End With
Next BorderIndex
For Each BorderIndex In Array(xlEdgeLeft)
With Range("R11:R35").Borders(BorderIndex)
.Weight = xlThick
.ColorIndex = vbBlack
End With
Next BorderIndex
End Sub

k0krd1.png
[/IMG]
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try this

Code:
Sub DynamicRangeBorders()
    Dim StartCell As Range
    Dim BorderIndex As Variant
[COLOR=#0000ff]    Dim lr As Long[/COLOR]
    
    Set StartCell = Range("A11")
    Cells.Borders.LineStyle = xlNone
    StartCell.CurrentRegion.Borders.LineStyle = xlContinuous
[COLOR=#0000ff]    lr = Range("A" & Rows.Count).End(xlUp).Row[/COLOR]
    For Each BorderIndex In Array(xlEdgeTop, xlEdgeLeft, xlEdgeRight)
        With Range([COLOR=#0000ff]"L11:R" & lr[/COLOR]).Borders(BorderIndex)
            .Weight = xlThick
            .ColorIndex = vbBlack
        End With
    Next BorderIndex
    For Each BorderIndex In Array(xlEdgeLeft)
        With Range([COLOR=#0000ff]"R11:R" & lr[/COLOR]).Borders(BorderIndex)
            .Weight = xlThick
            .ColorIndex = vbBlack
        End With
    Next BorderIndex
End Sub
 
Upvote 0
How about
Code:
Sub DynamicRangeBorders()
   Dim StartCell As Range
   Set StartCell = Range("A11")
   Cells.Borders.LineStyle = xlNone
   StartCell.CurrentRegion.Borders.LineStyle = xlContinuous
   Dim BorderIndex As Variant
   For Each BorderIndex In Array(xlEdgeTop, xlEdgeLeft, xlEdgeRight)
      With Range("L11", Range("R" & Rows.Count).End(xlUp)).Borders(BorderIndex)
         .Weight = xlThick
         .ColorIndex = vbBlack
      End With
   Next BorderIndex
   For Each BorderIndex In Array(xlEdgeLeft)
      With Range("R11", Range("R" & Rows.Count).End(xlUp)).Borders(BorderIndex)
         .Weight = xlThick
         .ColorIndex = vbBlack
      End With
   Next BorderIndex
End Sub
 
Upvote 0
You could also eliminate the loop in this way:

Code:
Sub DynamicRangeBorders()
    Dim StartCell As Range
    Dim lr As Long
    
    Set StartCell = Range("A11")
    Cells.Borders.LineStyle = xlNone
    StartCell.CurrentRegion.Borders.LineStyle = xlContinuous
    lr = Range("A" & Rows.Count).End(xlUp).Row
    With Range("L11:L" & lr & ",R11:R" & lr & ",S11:S" & lr)
        .Borders(xlEdgeLeft).Weight = xlThick
        .Borders(xlEdgeLeft).ColorIndex = vbBlack
    End With
    With Range("L11:R11")
        .Borders(xlEdgeTop).Weight = xlThick
        .Borders(xlEdgeTop).ColorIndex = vbBlack
    End With
End Sub
 
Upvote 0
Hi Fluff,

Thanks for your quick reply to my post yesterday. Both yours and Dante's solution did the trick.
As a person who learns best by doing, I always try to solve things myself before reaching the point where it’s time to learn from others. Fortunately, when the time to learn rolls around, I continue to receive great support from Mr. Excel Message Board members like you and Dante!

Kindest regards,

-John
 
Upvote 0
Hi Dante,

Thanks for your quick reply to my post yesterday. Both yours and Fluff's solution did the trick.
As a person who learns best by doing, I always try to solve things myself before reaching the point where it’s time to learn from others. Fortunately, when the time to learn rolls around, I continue to receive great support from Mr. Excel Message Board members like you and Fluff!

Kindest regards,

-John
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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