Borders without selection

tourless

Board Regular
Joined
Feb 8, 2007
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi Folks.

I'm betting there is a more efficient way do to this... possibly without having to select the cell(s)?
VBA Code:
Range("A22:J22").Select
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You could probably use:
VBA Code:
Sub tourless()
Dim i As Long

'Clear borders
Range("A22:J22").Borders.LineStyle = xlNone

'Add Edge borders
For i = 7 To 10
    With Range("A22:J22").Borders(i)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
Next i
End Sub
 
Upvote 0
Solution
You could probably use:
VBA Code:
Sub tourless()
Dim i As Long

'Clear borders
Range("A22:J22").Borders.LineStyle = xlNone

'Add Edge borders
For i = 7 To 10
    With Range("A22:J22").Borders(i)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
Next i
End Sub
Well that worked like a charm! One question for my own knowledge... why do you use 5-12 to remove then 7-10 to add?
 
Upvote 0
I actually adjusted the code... the 5-12 are all of the individual border index values (see the link to the Microsoft page I posted). I looped through them to clear them, but then realized a loop wasn't necessary to remove all borders. The loop from 7-10 is needed to set the individual Left, Right, Top, Bottom edges - as those index values correlate to the edges mentioned.
 
Upvote 0
I actually adjusted the code... the 5-12 are all of the individual border index values (see the link to the Microsoft page I posted). I looped through them to clear them, but then realized a loop wasn't necessary to remove all borders. The loop from 7-10 is needed to set the individual Left, Right, Top, Bottom edges - as those index values correlate to the edges mentioned.
I noticed that adjustment and that's good to know. I never knew about those index values. Thank you.
 
Upvote 0
What about adding all the borders at once?

VBA Code:
Sub tourless_v2()
  Range("A22:J22").Borders.LineStyle = xlNone
  Range("A22:J22").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, ColorIndex:=0
End Sub
 
Upvote 0
Range("A22:J22").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, ColorIndex:=0
That's nice and tight. I like that. Thank you everyone!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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