Macro to add Borders

MJA001

New Member
Joined
Dec 28, 2017
Messages
28
Hello,

I'm looking for a Macro to add Thick Outside Border to set width of columns, but will only extend down to the rows that are populated with info. In terms of my spreadsheet, The top of the Thick Outside Border will cover A5 to D5 and extend down for the cells that are populated. My gut tells me that this is an easy one. I just wish I was smart enough to figure it out. :confused:

Thanks in advance.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Perhaps something like this.
Code:
Sub AddThickBordersToCellsWithDataInThemInColumnsAToDFromRow5Down()
Dim LastRow As Long

    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    
    With Range("A5:D" & LastRow).Borders
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    
End Sub
 
Upvote 0
If I'm understanding what you're trying to do correctly, you could use conditional formatting to achieve the same thing - it's similar to something I did earlier today.

Conditional Formatting > New Rule > New Formatting Rule

From the list of rule types, select the last type on the list "Use a formula to determine which cells to format". In the box where you enter the format, use this formula:

Code:
=$A1<>""

Set your border format to thick outside border as you normally would formatting a cell or cells and press okay. In the "applies to" box on the list of rules, apply the format to
Code:
[COLOR=#242729][FONT=Consolas]=$A:$D[/FONT][/COLOR]
.

Your formula needs to refer to the first row of the Applied To area. It sounds counter-intuitive if the area you want the border around starts on row 5, but your formula needs to refer to the first row of your 'applied to' area.

I had a set of columns where I wanted borders around every cell that's populated in columns B to T (in your case simply select the thick outside border in the 'format cells' window rather than the 'all borders' I used). My data rows start on row 4, as above row 4 I have a title and column headings, so I assumed that I needed to refer to B4, but that caused all sorts of weird results! Now with that set up, every cell that's populated in columns B to T from row 4 downwards has borders and every time I add an additional row of data, the borders extend downwards, but only to the last row that's populated.

It worked for me, so hopefully with that little tweak of the formatting it should work for you.
 
Upvote 0
Thanks! This worked great. How can I modify this to add the border to only the outside edge of the entire range?
 
Upvote 0
For that you can use BorderAround.
Code:
Dim LastRow As Long

    LastRow = Range("A" & Rows.Count).End(xlUp).Row

    Range("A5:H" & LastRow).BorderAround LineStyle:=xlContinuous, Weight:=xlThick
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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