Borders - non-contiguous ranges

R

Richard

Hello,

The following macro places borders around the range
A6:D50:

Sub myborders()
Dim ws As Worksheet
Dim Lastrow As Integer

Set ws = Sheets(1)
Lastrow = 50

With ws.Range("A6:D" & Lastrow)
With .Borders
.LineStyle = xlBorderLineStyleContinuous
.Weight = xlThin
End With
End With

End Sub

However, the project has several non-contiguous ranges
that require formatting with borders. I wish to achieve
this without repeating for each range, the code starting
with "With ws.range("A6:D" & Lastrow)".

This is the type of thing that I am looking for (this
syntax does not work):
With ws.range("A6:D" & Lastrow, "K6:L" & Lastrow, "P6:S"
& LastRow)

I will appreciate any suggestions on the above matter.

TIA

Richard
 
R

Richard

Thank Tom, works perfectly. I tried something like that
but obviously had the apostrophes in the wrong place.
I've just replace about 50 lines of code with 6.

Regards,

Richard
 
J

Jamal

Tom,

If i wnanted to just put outside boarders (and leave
inside as it is)for non-contiguous ranges , what code
would I need? Thanks.
 
R

Richard

Jamal,

Using Tom's code snippet, this will place a border around
A6:D50, K6:L50, P6:S50. Change to suit. See the VBA Help
file for the BorderAround method, in particular, the
properties to change the line style, line weight and
color.

Sub myBordersAround()
Dim lastrow As Integer
Dim ws As Worksheet
Dim rng As Range

lastrow = 50
Set ws = Sheets(1)
Set rng = ws.Range("A6:D" & lastrow & ",K6:L" & lastrow
& ",P6:S" & lastrow)

' Clear existing borders
' Cells.Borders.LineStyle = xlLineStyleNone

're-establish borders
'rng.BorderAround , , ColorIndex:=xlColorIndexAutomatic
rng.BorderAround LineStyle:=xlContinuous,
Weight:=xlThick, _
ColorIndex:=xlColorIndexAutomatic

End Sub

Regards,
Richard
-----Original Message-----
Tom,

If i wnanted to just put outside boarders (and leave
inside as it is)for non-contiguous ranges , what code
would I need? Thanks.
 
J

Jamal

Richard,
This is very helpful. It changed lot of lines od codes to
a few. Thanks.
-----Original Message-----
Jamal,

Using Tom's code snippet, this will place a border around
A6:D50, K6:L50, P6:S50. Change to suit. See the VBA Help
file for the BorderAround method, in particular, the
properties to change the line style, line weight and
color.

Sub myBordersAround()
Dim lastrow As Integer
Dim ws As Worksheet
Dim rng As Range

lastrow = 50
Set ws = Sheets(1)
Set rng = ws.Range("A6:D" & lastrow & ",K6:L" & lastrow
& ",P6:S" & lastrow)

' Clear existing borders
' Cells.Borders.LineStyle = xlLineStyleNone

're-establish borders
'rng.BorderAround , , ColorIndex:=xlColorIndexAutomatic
rng.BorderAround LineStyle:=xlContinuous,
Weight:=xlThick, _
ColorIndex:=xlColorIndexAutomatic

End Sub

Regards,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

revise CONCATENATE code 2
CONCATENATE code 2
Code copies twice...? 13
Border formatting row of cells 22
My Macro is slower than the turtle 1
Cut and remove last text 4
help needed with borders 2
Macro storing as a date? 11

Top