Code to select column and insert border at each change in data

stevod

Board Regular
Joined
Aug 21, 2013
Messages
67
Hi Guys im after some code that can add a border to the end of the change in data but defined by a specific column.

so in theory it would open a message box to determine the column to look down and at the end of each change in data insert a border.
The border would be from A:N for example

I've got some code that work for a defined range but i want a msg box adding to it

Code:
Sub AddBorderLineWhenValueChanges_PO()'add border
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim xrg As Range
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each xrg In Range("AC2:AC" & LastRow)
        If xrg <> xrg.Offset(1, 0) Then
            Range("A" & xrg.Row & ":N" & xrg.Row).Borders(xlEdgeBottom).LineStyle = xlDouble
        End If
    Next xrg
    Application.ScreenUpdating = True
End Sub

any help you could give would be awesome!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
To have the user select which column to look down.

Code:
Sub AddBorderLineWhenValueChanges_PO()'add border
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim xrg As Range
    [COLOR="#FF0000"]Dim uiColumn As Range[/COLOR]    

    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    [COLOR="#FF0000"]On Error Resume Next
    Set uiColumn = Application.InputBox("Select a column", type:=8).EntireColumn
    On Error Resume Next
    If uiColumn is Nothing Then Exit Sub: Rem user canceled
[/COLOR]
    For Each xrg In [COLOR="#FF0000"]uiColumn.Resize(LastRow, 1)[/COLOR]
        If xrg <> xrg.Offset(1, 0) Then
            Range("A" & xrg.Row & ":N" & xrg.Row).Borders(xlEdgeBottom).LineStyle = xlDouble
        End If
    Next xrg
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Works well, it stops on the first line, i want it to do it for the entire page of data, xldown function


To have the user select which column to look down.

Code:
Sub AddBorderLineWhenValueChanges_PO()'add border
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim xrg As Range
    [COLOR=#FF0000]Dim uiColumn As Range[/COLOR]    

    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    [COLOR=#FF0000]On Error Resume Next
    Set uiColumn = Application.InputBox("Select a column", type:=8).EntireColumn
    On Error Resume Next
    If uiColumn is Nothing Then Exit Sub: Rem user canceled
[/COLOR]
    For Each xrg In [COLOR=#FF0000]uiColumn.Resize(LastRow, 1)[/COLOR]
        If xrg <> xrg.Offset(1, 0) Then
            Range("A" & xrg.Row & ":N" & xrg.Row).Borders(xlEdgeBottom).LineStyle = xlDouble
        End If
    Next xrg
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try
Code:
For each xrng in uiColumn.Resize(LastRow, 1).Cells
Also, what is the value of lastRow?
 
Upvote 0
Deleted as OP has amended their previous post so now irrelevant.
 
Last edited:
Upvote 0
another note... instead of defining the row length to apply the border could it find the end of the data itself?

Range("A" & xrg.Row & ":N" & xrg.Row).Borders(xlEdgeBottom).LineStyle = xlDouble
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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