Avoid using Select and Selection

rex759

Well-known Member
Joined
Nov 8, 2004
Messages
610
Office Version
  1. 365
Platform
  1. Windows
Hello,
The code below does what I want, it adds boarders to ranges in sections that contain text. If a row is blank, is doesn’t add the boarders which is exactly what I want it to do. However, I don’t want to be on the active sheet and trying to work around the Select and Selection commands.

I cant seem to get past this line:
VBA Code:
WS.Range(Cells(R, c), Cells(R, lngLstCol)).Select

Full Code
VBA Code:
Sub addboarders()
Application.ScreenUpdating = False

Dim lngLstCol As Long, lngLstRow As Long
Dim WS As Worksheet

Set WS = Sheet4

lngLstRow = WS.UsedRange.Rows.Count

lngLstCol = WS.UsedRange.Columns.Count



For Each rngCell In WS.Range("A2:A" & lngLstRow)

If rngCell.Value > "" Then
R = rngCell.Row
c = rngCell.Column

WS.Range(Cells(R, c), Cells(R, lngLstCol)).Select

With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With

End If

Next
Application.ScreenUpdating = True

End Sub

Any help is appreciated
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about
VBA Code:
With ws.Range(ws.Cells(r, c), ws.Cells(r, lngLstCol)).Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
 
Upvote 0
Solution
Hello and thank you for that quick response.

I replaced just this section:

VBA Code:
WS.Range(Cells(R, c), Cells(R, lngLstCol)).Select
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic

End With

with your code and I didnt get an error message but it did not apply the boarders.

VBA Code:
For Each rngCell In WS.Range("A2:A" & lngLstRow)
    If rngCell.Value > "" Then
        R = rngCell.Row
        c = rngCell.Column
        
        With WS.Range(WS.Cells(R, c), WS.Cells(R, lngLstCol)).Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        
        'WS.Range(Cells(R, c), Cells(R, lngLstCol)).Select
        '    With Selection.Borders
        '        .LineStyle = xlContinuous
        '        .Weight = xlThin
        '        .ColorIndex = xlAutomatic
        '    End With
    End If
Next
 
Upvote 0
That code works for me. Can you post your entire code.
 
Upvote 0
I apologized sir, it works perfectly. I have many test sheets in this workbook and was looking at a different tab when I ran the code.
In retrospect, I was close but missed the second set of WS.Cells.

Thank you very much.
 
Upvote 0
Glad it's sorted & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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