Why won't my code run on each worksheet?

Dr_Beanie

New Member
Joined
May 11, 2017
Messages
1
I am trying to get this code to loop through each worksheet...but it doesn't seem to want to!

Code:
Sub BorderUsedCells()

For Each ws In Worksheets


    For Each rngCell In Range("A5:E25")
    If rngCell.Value > "" Then
        r = rngCell.Row
        c = rngCell.Column
        Range(Cells(r, c), Cells(r, c)).Select
            With Selection.Borders
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
            End If
            Next
            
Next ws
End Sub

Thanks for your help
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Each ws needs to be referenced within the code.
Also, Select can only be used on the active worksheet (otherwise run-time error 1004 will occur) :

Code:
Sub BorderUsedCells()
For Each ws In Worksheets
    For Each rngCell In ws.Range("A5:E25")
        If rngCell.Value > "" Then
            With rngCell.Borders
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
        End If
    Next
Next ws
End Sub

This code is more efficient (no cell loop) :

Code:
Sub BorderUsedCells()
Dim rng As Range
On Error Resume Next
For Each ws In Worksheets
    Set rng = Union(ws.[A5:E25].SpecialCells(xlCellTypeFormulas, 23), _
        ws.[A5:E25].SpecialCells(xlCellTypeConstants, 23))
    With rng.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
Next
On Error GoTo 0
End Sub
 
Last edited:
Upvote 0
Correction :

Code:
Sub BorderUsedCells()
Dim ws As Worksheet, rng As Range
On Error Resume Next
For Each ws In Worksheets
    Set rng = ws.[A5:E25]
    With rng.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    On Error Resume Next
    rng.SpecialCells(xlCellTypeBlanks) _
        .Borders.LineStyle = xlNone
    With Application
        .ReplaceFormat.Clear
        .ReplaceFormat.Borders.LineStyle = xlNone
        rng.SpecialCells(xlCellTypeFormulas, 23). _
            Replace """", "", SearchFormat:=False, _
            ReplaceFormat:=True
        .ReplaceFormat.Clear
    End With
Next
On Error GoTo 0
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
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