VBA: Drawing Borders (Tidy-up Code)

dr_drews

New Member
Joined
Dec 18, 2004
Messages
16
Hi,

Does anyone know a good way to tidy-up the following code. I am trying to draw borders around various different ranges, but as I'm doing this on about 15 different ranges the code takes up a lot of space (and probably isn't very efficient). This is a sample of what I have at the moment:

With .Range(Cells(4, "D"), Cells(Counter, "D"))
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
With .Range(Cells(4, "F"), Cells(Counter, "F"))
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
With .Range(Cells(4, "G"), Cells(Counter, "G"))
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Just make your range the whole thing at once.

Code:
With .Range(Cells(4, "D"), Cells(Counter, "G"))
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
 
Upvote 0
That helps, thanks. Means I'd end up with this as I don't want to border certain ranges (column I for example). Is this the tidiest it can be?

With .Range(Cells(4, "D"), Cells(Counter, "H"))
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
With .Range(Cells(4, "J"), Cells(Counter, "O"))
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
With .Range(Cells(Counter, "A"), Cells(Counter, "H"))
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
With .Range(Cells(Counter, "J"), Cells(Counter, "O"))
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
 
Upvote 0
Sorry, should have tested that first. It doesn't actually work as it just draws the border round the outside of the whole range, but I need to border all the columns within that range.
 
Upvote 0
Sorry, should have tested that first. It doesn't actually work as it just draws the border round the outside of the whole range, but I need to border all the columns within that range.

Add

Code:
.Borders (xlInsideVertical).Weight = xlMedium

and possibly

Code:
.Borders (xlInsideHorizontal).Weight = xlMedium

Depending on what you want.
 
Upvote 0
If there was some kind of pattern to your rows and columns that needed borders, you could use a loop, similar to the one shown below. You would create an array of the column letters you want borders around (adjust the array size to the correct number of elements, then specify the elements as shown). Not sure based on your example code that you could get to something like this though.
Code:
Sub drews()
Dim counter As Long, arr(1 To 3) As String
arr(1) = "D": arr(2) = "F": arr(3) = "G"
counter = 2  ' Just for testing purposes

For i = 1 To UBound(arr)
    With Range(Cells(4, arr(i)), Cells(counter, arr(i)))
        .Borders(xlEdgeBottom).Weight = xlMedium
        .Borders(xlEdgeTop).Weight = xlMedium
        .Borders(xlEdgeLeft).Weight = xlMedium
        .Borders(xlEdgeRight).Weight = xlMedium
    End With
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

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