VBA To Add Borders

jo15765

Well-known Member
Joined
Sep 23, 2011
Messages
591
Greetings - this works, but is more of an optimization question. I am wanting to add borders (all) around my selected range. Is there a shorter way to write this code? This is what the Macro Recorder created
Code:
Range("A1:" & colName & lRow).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Color = -16776961
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Color = -16776961
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Color = -16776961
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Color = -16776961
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Color = -16776961
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Color = -16776961
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Range("A1").Select
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
something like
Code:
Selection.BorderAround LineStyle:=xlContinuous, Weight:=xlThin, Color:=RGB(255, 0, 0)
 
Upvote 0
Hi,

try

Code:
Sub FormatBorders(ByVal Target As Range)
'
' Format Macro
'
'
    With Target
        .ClearFormats
'inside
        .Borders.ColorIndex = 3
        .Borders.Weight = xlThin
'outside
        .BorderAround Weight:=xlThick, ColorIndex:=3
    End With
End Sub


to call it

Code:
FormatBorders Range("A1:" & colname & lRow)

Dave
 
Last edited:
Upvote 0
@Dave -> this only formats row 1 - not the used range....

Disregard, I had to qualify one of my variables with .ws -- this works as desired.. Thanks!
 
Last edited:
Upvote 0
@Dave -> this only formats row 1 - not the used range....

Disregard, I had to qualify one of my variables with .ws -- this works as desired.. Thanks!

Sub will format the range you pass to it

example

Code:
FormatBorders Range("D10:H20")

should format the specified range

Example I first posted was based on your original code & assumes that your variables hold the required range values.
If you are doing something different then post it here.

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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