More efficient way to write this without using "Select"?

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I'm using select twice to achieve my result of adding horizontal boarders. Is there perhaps a better way to write this?

The far right column can change, as can the bottom row.

VBA Code:
    Range(Range("A4"), Range("A4").End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlEdgeTop).LineStyle = xlContinuous
    Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous
    Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous

Thank you
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Maybe this way
VBA Code:
Sub MM1()
Dim lr As Long, lc As Integer
lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
Range(Cells(4, 1), Cells(lr, lc)).Borders(xlInsideHorizontal).LineStyle = xlContinuous
End Sub
 
Upvote 0
Solution
doesn't work with row 1 !
VBA Code:
    With Range("A4")                                           'your topleftcell
          k = .End(xlToRight).Column                            'last column number
          r = .End(xlDown).Row                                  'last row number
          .Offset(-1).Resize(r - .Row + 3, k).Borders(xlInsideHorizontal).LineStyle = xlContinuous     'go 1 row higher and number of row +3 then everything is inside
     End With
 
Upvote 0
VBA Code:
Range("A4").UsedRange.Select
  With Selection
    .Borders(xlEdgeTop).LineStyle = xlContinuous
    .Borders(xlEdgeBottom).LineStyle = xlContinuous
    .Borders(xlInsideHorizontal).LineStyle = xlContinuous
End With
 
Upvote 0
Maybe this way
VBA Code:
Sub MM1()
Dim lr As Long, lc As Integer
lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
Range(Cells(4, 1), Cells(lr, lc)).Borders(xlInsideHorizontal).LineStyle = xlContinuous
End Sub
Fantastic, thank you.

I get lr as Long, why lc as Integer and not Long? Does it have something to do with it representing columns and not rows?
 
Upvote 0
VBA Code:
Range("A4").UsedRange.Select
  With Selection
    .Borders(xlEdgeTop).LineStyle = xlContinuous
    .Borders(xlEdgeBottom).LineStyle = xlContinuous
    .Borders(xlInsideHorizontal).LineStyle = xlContinuous
End With
This looked interesting, but on the first line I get Object doesn't support this property or method. Are you using 365? I'm on 2016 :(

Thank you
 
Upvote 0
Rich (BB code):
Does it have something to do with it representing columns and not rows?
Correct !!
 
Upvote 0
there are +16,000 columns and +1,000,000 in a worksheet
integer is until +32,000, so columns are okay with integers, rows aren't !!!

replace .usedrange by .currentregion
 
Upvote 0
there are +16,000 columns and +1,000,000 in a worksheet
integer is until +32,000, so columns are okay with integers, rows aren't !!!

replace .usedrange by .currentregion
Thank you for the additional info!

Much appreicated
 
Upvote 0
This looked interesting, but on the first line I get Object doesn't support this property or method. Are you using 365? I'm on 2016 :(

Thank you
Not test with PC. But try again:
VBA Code:
  With Range("A4").CurrentRegion
    .Borders(xlEdgeTop).LineStyle = xlContinuous
    .Borders(xlEdgeBottom).LineStyle = xlContinuous
    .Borders(xlInsideHorizontal).LineStyle = xlContinuous
End With
 
Upvote 0

Forum statistics

Threads
1,214,635
Messages
6,120,660
Members
448,975
Latest member
sweeberry

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