Copy a range of cells above / below the last one with a border

OfficeUser

Well-known Member
Joined
Feb 4, 2010
Messages
542
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a spreadsheet which has a border on the bottom of cells in row 32-37 (only one of those rows has this). I want to copy all the cells above this.

For example, Row 37 has a border on the bottom of the cells, I want to then copy range A4:M36.

The same goes for the range below that border, I want to then copy range A38:M62. Is this something that is able to be done? Thanks!!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try:
Rich (BB code):
Sub Underlineborder()

    Dim x       As Long
    Dim LR      As Long
    Dim arrT()  As Variant
    Dim arrB()  As Variant
    
    With Sheets("Sheet1")
        LR = .Cells(.Rows.count, 1).End(xlUp).row
    
        For x = 32 To 37
            If .Cells(x, 1).Borders(xlEdgeBottom).LineStyle <> xlLineStyleNone Then Exit For
        Next x
    
        arrT = .Cells(4, 1).Resize(x - 4, 13).Value
        arrB = .Cells(x + 1, 1).Resize(LR - x, 13).Value
    End With
    
    Sheets("Sheet2").Cells(1, 1).Resize(UBound(arrT, 1), UBound(arrT, 2)).Value = arrT
    Sheets("Sheet3").Cells(1, 1).Resize(UBound(arrB, 1), UBound(arrB, 2)).Value = arrB
    
    Erase arrT
    Erase arrB

End Sub
 
Last edited:
Upvote 0
Try:
Rich (BB code):
Sub Underlineborder()

    Dim x       As Long
    Dim LR      As Long
    Dim arrT()  As Variant
    Dim arrB()  As Variant
    
    With Sheets("Sheet1")
        LR = .Cells(.Rows.count, 1).End(xlUp).row
    
        For x = 32 To 37
            If .Cells(x, 1).Borders(xlEdgeBottom).LineStyle <> xlLineStyleNone Then Exit For
        Next x
    
        arrT = .Cells(4, 1).Resize(x - 4, 13).Value
        arrB = .Cells(x + 1, 1).Resize(LR - x, 13).Value
    End With
    
    Sheets("Sheet2").Cells(1, 1).Resize(UBound(arrT, 1), UBound(arrT, 2)).Value = arrT
    Sheets("Sheet3").Cells(1, 1).Resize(UBound(arrB, 1), UBound(arrB, 2)).Value = arrB
    
    Erase arrT
    Erase arrB

End Sub

The one thing it does is not copy the row if the border is on the bottom of the row. For example, if Row 32 has a bottom border, it will only copy to Row 31. I explained it incorrectly, otherwise it works great, I appreciate it!!!
 
Last edited:
Upvote 0
Try:
Code:
Sub Underlineborder()

    Dim x       As Long
    Dim LR      As Long
    Dim arrT()  As Variant
    Dim arrB()  As Variant
        
    With Sheets("Sheet1")
        LR = .Cells(.Rows.count, 1).End(xlUp).row
        .Cells(4, 15).Resize(LR, 13).ClearContents
        For x = 32 To 37
            If .Cells(x, 1).Borders(xlEdgeBottom).LineStyle <> xlLineStyleNone Then Exit For
        Next x
        
        arrT = .Cells(4, 1).Resize(x - 3, 13).Value
        arrB = .Cells(x + 1, 1).Resize(LR - x, 13).Value
    End With
    
    Sheets("Sheet2").Cells(1, 1).Resize(UBound(arrT, 1), UBound(arrT, 2)).Value = arrT
    Sheets("Sheet3").Cells(1, 1).Resize(UBound(arrB, 1), UBound(arrB, 2)).Value = arrB

    Erase arrT
    Erase arrB

End Sub
 
Last edited:
Upvote 0
It works great!! Something as simple as this will save about 4 hrs. per week. Thanks again!!
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,175
Members
448,870
Latest member
max_pedreira

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