VBA Code to clear cell borders

learningexcel123

New Member
Joined
May 1, 2010
Messages
13
I'm sure there's a quicker way and also not sure what is wrong with my code but I need the code to loop through a workbook, making changes only to the worksheets listed in the array. The change needed is to clear all borders in cells F62:F165 with the exception of the right border. My code clears all borders first and then adds right border back in. The problem is that the code only changes the cells in the active sheet I am on and not the ones specified in the array. Secondly, I'm hoping there's a shorter code out there:

Sub Clear_Borders()
Dim ws As Worksheet
For Each ws In Sheets(Array("Sheet1", "Sheet2"))
Range("F62:F165").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
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
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Next ws
End Sub

Thanks for your help!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
You need to address your commands to the ws objects directly so the commands occur ON those sheets. Also, you can always abbreviate code relating to borders and remove the "selecting" from your recorded macros:
Rich (BB code):
 Option Explicit

Sub Clear_Borders()
Dim ws As Worksheet

    For Each ws In Sheets(Array("Sheet1", "Sheet2"))
        With ws.Range("F62:F165")
            .Borders.LineStyle = xlNone
            .Borders(xlEdgeRight).Weight = xlThin
        End With
    Next ws

End Sub

The line of code in red doesn't single out any one specific border, so it applies that command to ALL of them.
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

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