VBA code to alter specific range to selection

heathball

Board Regular
Joined
Apr 6, 2017
Messages
135
Office Version
  1. 365
Platform
  1. Windows
This code is working well. I want to improve it.

I have two goals.

I cannot make any impact when i try to solve this, which makes me think it needs a different approach if I am going to achieve it.

1. it currently requires a range to be entered. eg. bt2:bt
For Each xrg In Range("bt2:bt" & LastRow - 1)

I am trying to get it to work on the "selection" -which in practice would normallly be the selection of an entire column

for eg, something like a
with selection
'code in here'
end with

may work, but did not work for me.


2.
with this required range for the choice of how far the border extends (left to right) eg. LK
Range("A" & xrg.row & ":LK" & xrg.row).BORDERS(xlEdgeBottom).Weight = xlMedium

Can this be set to choose the "LastColumn (last column or cell used) plus the next 50 columns" and not require the actual cell reference to be input.



Hope someone can help, Thanks in advance.


VBA Code:
Sub BORDER_CRITERIA()
'CREATES A ROW BORDER AT THE POINT WHERE THERE IS A CELL CHANGE WITHIN A COLUMN

 
 

     Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim xrg As Range
    
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
    For Each xrg In Range("bt2:bt" & LastRow - 1)
        If xrg <> xrg.Offset(1, 0) Then
            Range("A" & xrg.row & ":LK" & xrg.row).BORDERS(xlEdgeBottom).Weight = xlMedium
            
        End If
    Next xrg
    Application.ScreenUpdating = True
End Sub
 
If you want to add a border to the last row, why not just change:
VBA Code:
LastRow - 1
to
VBA Code:
LastRow

Or if you want to pull the row from the selected cell, you could use:
VBA Code:
For Each xrg In Range("bt2:bt" & Selection.Row)
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,224,823
Messages
6,181,173
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