VBA - Dynamic range needs border

ChrisFoster

Board Regular
Joined
Jun 21, 2019
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a dynamic range of rows across columns F:K, there are some cells in columns J & K though that may be blank at random points. What I need is some code that will put a border round all cells across the rows that has data in column F, then add a bold border round that whole table.

Is this even possible? If so, any help would be greatly appreciated.

Kind Regards,

Chris
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Assumes
- the "data" in F is NOT formula
- row1 is header row

VBA Code:
Sub Border()
    Dim r As Long, c As Long
    Range("F2:F" & Rows.Count).SpecialCells(xlCellTypeConstants).BorderAround ColorIndex:=5, Weight:=xlThin
    For c = 6 To 11
        r = WorksheetFunction.Max(r, Cells(Rows.Count, c).End(xlUp).Row)
    Next
    Range("F1:K1").Resize(r).BorderAround ColorIndex:=23, Weight:=xlThick
End Sub
 
Upvote 0
Assumes
- the "data" in F is NOT formula
- row1 is header row

VBA Code:
Sub Border()
    Dim r As Long, c As Long
    Range("F2:F" & Rows.Count).SpecialCells(xlCellTypeConstants).BorderAround ColorIndex:=5, Weight:=xlThin
    For c = 6 To 11
        r = WorksheetFunction.Max(r, Cells(Rows.Count, c).End(xlUp).Row)
    Next
    Range("F1:K1").Resize(r).BorderAround ColorIndex:=23, Weight:=xlThick
End Sub
Thanks for your help. This has partly done what I need but has only put a thin border round F2:F and then thick around the whole thing. What I need it to look like is shown in the attached photo please.

Table.jpg


Is this possible?
 
Upvote 0
Here you go

VBA Code:
Sub Border()
    Dim r As Long, c As Long, cel As Range
    For c = 6 To 11
        r = WorksheetFunction.Max(r, Cells(Rows.Count, c).End(xlUp).Row)
    Next
'thin lines around all cells   
    For Each cel In Range("F1:K1").Resize(r)
        cel.BorderAround ColorIndex:=23, Weight:=xlThin
    Next
'thick border around the whole thing
    Range("F2:K" & r).BorderAround ColorIndex:=23, Weight:=xlThick
'thick border around row1
    Range("F1:K1").BorderAround ColorIndex:=23, Weight:=xlThick
'thin left border to column F
    For Each cel In Range("F2:F" & r)
        With cel.Borders(xlEdgeLeft)
            .ColorIndex = 23
            .Weight = xlThin
        End With
    Next
End Sub
 
Upvote 0
Here you go

VBA Code:
Sub Border()
    Dim r As Long, c As Long, cel As Range
    For c = 6 To 11
        r = WorksheetFunction.Max(r, Cells(Rows.Count, c).End(xlUp).Row)
    Next
'thin lines around all cells  
    For Each cel In Range("F1:K1").Resize(r)
        cel.BorderAround ColorIndex:=23, Weight:=xlThin
    Next
'thick border around the whole thing
    Range("F2:K" & r).BorderAround ColorIndex:=23, Weight:=xlThick
'thick border around row1
    Range("F1:K1").BorderAround ColorIndex:=23, Weight:=xlThick
'thin left border to column F
    For Each cel In Range("F2:F" & r)
        With cel.Borders(xlEdgeLeft)
            .ColorIndex = 23
            .Weight = xlThin
        End With
    Next
End Sub
This is so very close, the only issue is that the left of column F is a think line rather than a thick line (see attachment). Any ideas how to solve that?

P.S Thanks for helping me with this.

Table.jpg
 
Upvote 0
Another option
VBA Code:
Sub ChrisFoster()
    With Range("F1:K" & Range("F" & Rows.Count).End(xlUp).Row)
      .Borders.ColorIndex = 23
      .Borders.Weight = xlThin
      .BorderAround , xlThick, 23
      .Resize(1).Borders(xlEdgeBottom).Weight = xlThick
   End With
End Sub
 
Upvote 0
that sentence makes no sense :unsure:
Sorry, I meant thin line not think line.

Another option
VBA Code:
Sub ChrisFoster()
    With Range("F1:K" & Range("F" & Rows.Count).End(xlUp).Row)
      .Borders.ColorIndex = 23
      .Borders.Weight = xlThin
      .BorderAround , xlThick, 23
      .Resize(1).Borders(xlEdgeBottom).Weight = xlThick
   End With
End Sub

This has worked perfectly, thank-you.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,496
Members
449,089
Latest member
Raviguru

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