Borders With Inside Lines Macro

miketurn

Active Member
Joined
Dec 8, 2016
Messages
268
Hello Everyone,

I would imagine this to be a very easy fix for the skilled members of this forum, but I personally do not know how to do so.

I created the following simple macro that applies border lines to select cells.
It adds a border around the edges, as well as adds border lines to vertical cell lines (NOT horizontal lines) within the selection.
This tools works fines, the only issue is that if you only select a single COLUMN to add a border around, because their are no "inside" lines to add borders to, it produces an error....

Code:
Run-time error '1004':
Unable to set the LineStyle property of the Border class

Can someone edit my following macro, so that it can ignore trying to add the "inside" lines when not present in the selection?

Code:
Sub Border_Inside&Out() 
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone 
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone 
    With Selection.Borders(xlEdgeLeft) 
        .LineStyle = xlContinuous 
        .Weight = xlMedium 
        .ColorIndex = xlAutomatic 
    End With 
    With Selection.Borders(xlEdgeTop) 
        .LineStyle = xlContinuous 
        .Weight = xlMedium 
        .ColorIndex = xlAutomatic 
    End With 
    With Selection.Borders(xlEdgeBottom) 
        .LineStyle = xlContinuous 
        .Weight = xlMedium 
        .ColorIndex = xlAutomatic 
    End With 
    With Selection.Borders(xlEdgeRight) 
        .LineStyle = xlContinuous 
        .Weight = xlMedium 
        .ColorIndex = xlAutomatic 
    End With 
    With Selection.Borders(xlInsideVertical) 
        .LineStyle = xlContinuous 
        .Weight = xlMedium 
        .ColorIndex = xlAutomatic 
    End With 
End Sub

Thank You
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Test for the number of columns...

Code:
Sub Border_InsideOut()
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    If Selection.Columns.Count > 1 Then
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
    End If
End Sub
 
Upvote 0
D a m n that was fast! I didn't even have to leave the forum and come back.
I have tested it out and it works great!
Thank you so much for this update.
 
Upvote 0
Another possible solution.

Code:
Sub Test()
Dim myBorders() As Variant, item As Variant
 myBorders = Array(xlEdgeLeft, _
 xlEdgeTop, _
 xlEdgeBottom, _
 xlEdgeRight, _
 xlInsideVertical)
  
'<~~ Remove Boarders
For Each item In myBorders
 With Selection.Borders(item)
 .LineStyle = xlNone
 End With
Next item
 
'<~~ Add Boarders
For Each item In myBorders
With Selection.Borders(item)
 .LineStyle = xlContinuous
 .Weight = xlMedium
 .ColorIndex = xlAutomatic
End With
Next item
End Sub
 
Upvote 0
@Biz
Thank you for your response, when I gave it a try your macro showed the same "1004" mentioned above, when it is applied to a single column that contains no inside lines?
 
Upvote 0
Test for the number of columns...

Code:
Sub Border_InsideOut()
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
 [COLOR="#FF0000"][B]   With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With[/B][/COLOR]
    If Selection.Columns.Count > 1 Then
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
    End If
End Sub
Suggestion: Replace all the red code with this single line
Rich (BB code):
Selection.BorderAround xlContinuous, xlMedium, xlAutomatic
 
Upvote 0
BTW, I don't get the error with your original code even if a single column is chosen. What version of Excel are you using?

Instead of testing for more than one column, I think this should also work for you
Rich (BB code):
Selection.BorderAround xlContinuous, xlMedium, xlAutomatic
<del>With Selection.Borders(xlInsideVertical)</del>
With Selection.Borders(xlLeft)
  .LineStyle = xlContinuous
  .Weight = xlMedium
  .ColorIndex = xlAutomatic
End With
 
Last edited:
Upvote 0
Suggestion: Replace all the red code with this single line
Rich (BB code):
Selection.BorderAround xlContinuous, xlMedium, xlAutomatic

I would have done the same if doing it myself but at the time was just seeing if the column test solved the issue the OP had that I wasn't getting :biggrin:
 
Last edited:
Upvote 0
@Peter_SSs
Thank You for the updates you provided, I went with the following code which works great

Code:
Sub Border_InsideOut()
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    [COLOR=#0000CD][B]Selection.BorderAround xlContinuous, xlMedium, xlAutomatic[/B][/COLOR]
    If Selection.Columns.Count > 1 Then
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
    End If
End Sub


The update you mention with the following code does work, but this method only puts one line in the inside borders, as where the other method above overlaps them.

Code:
[COLOR=#0000CD][B]With Selection.Borders([U]xlLeft[/U])[/B][/COLOR]

I move cell information around a great deal and I am constantly dragging those moved cells borders along with that information, so having that overlap helps in my situation, so that I don't have to keep adding those missing border lines to the right and left of my cells back.

Using features such as borders and color fill backgrounds in general, I am constantly having to fix the cell formats to make them look pretty again and again. As I mention above with the undesired dragging of the borders and I also made a post a while back, about wishing there was a way to "CUT" cell data and background fill color, but also leave the same background fill color behind, instead of leaving a white cell that has to be re-filled with a background color.

https://www.mrexcel.com/forum/excel-questions/979939-cut-cell-data-but-dont-remove-fill-color.html

Anyways, thank you again for the help you have provided here, as well as all the help you have provided me in the past, it is very appreciated.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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