Form Control Macro for copying the last row of data to the end

TGirl66

New Member
Joined
Dec 6, 2024
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I need a macro button to copy the last row of filtered data and add it to the end. In this example, the the macro would copy row 10 (TLP Kenswick) and copy/paste it to Row11.

1733514605633.png


Filtered here it would take Conroe (row 8) and also pasting to the open row (11). Is this possible? Thank you so much!

1733514752098.png
 

Attachments

  • 1733514543581.png
    1733514543581.png
    25.9 KB · Views: 8
Last edited by a moderator:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try the following code, which needs to be placed in a regular module (Visual Basic Editor [Alt+F11] >> Insert >> Module). Note that it assumes that the workbook running the code contains the data, and that the data is contained on Sheet1. Change the sheet name accordingly.

VBA Code:
Option Explicit

Sub CopyLastRowFilter()

    Dim targetSheet As Worksheet
    Dim dataFilterRange As Range
    Dim firstColumn As Long
    Dim nextRow As Long
 
    Set targetSheet = ThisWorkbook.Worksheets("Sheet1") 'change the worksheet name accordingly
 
    If targetSheet.FilterMode = False Then
        MsgBox "No filter applied.", vbExclamation
        Exit Sub
    End If
 
    With targetSheet.AutoFilter.Range
 
        On Error Resume Next
        Set dataFilterRange = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        If dataFilterRange Is Nothing Then
            MsgBox "No records found.", vbExclamation
            Exit Sub
        End If
        On Error GoTo 0
     
        firstColumn = .Cells(1, 1).Column
     
        nextRow = .Cells(.Rows.Count, 1).Row + 1
     
    End With
 
    With dataFilterRange
        With .Areas(.Areas.Count)
            .Rows(.Rows.Count).Copy targetSheet.Cells(nextRow, firstColumn)
        End With
    End With
 
End Sub

Hope this helps!
 
Last edited:
Upvote 0
Try the following code, which needs to be placed in a regular module (Visual Basic Editor [Alt+F11] >> Insert >> Module). Note that it assumes that the workbook running the code contains the data, and that the data is contained on Sheet1. Change the sheet name accordingly.

VBA Code:
Option Explicit

Sub CopyLastRowFilter()

    Dim targetSheet As Worksheet
    Dim dataFilterRange As Range
    Dim firstColumn As Long
    Dim nextRow As Long
 
    Set targetSheet = ThisWorkbook.Worksheets("Sheet1") 'change the worksheet name accordingly
 
    If targetSheet.FilterMode = False Then
        MsgBox "No filter applied.", vbExclamation
        Exit Sub
    End If
 
    With targetSheet.AutoFilter.Range
 
        On Error Resume Next
        Set dataFilterRange = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        If dataFilterRange Is Nothing Then
            MsgBox "No records found.", vbExclamation
            Exit Sub
        End If
        On Error GoTo 0
    
        firstColumn = .Cells(1, 1).Column
    
        nextRow = .Cells(.Rows.Count, 1).Row + 1
    
    End With
 
    With dataFilterRange
        With .Areas(.Areas.Count)
            .Rows(.Rows.Count).Copy targetSheet.Cells(nextRow, firstColumn)
        End With
    End With
 
End Sub

Hope this helps!
Hi Domenic, thank you for this information. Seems like we are close. once I enter this in vba can I enter a control botton and assign the macro? When I did this I recieved this error. Any thoughts?
1733671726321.png
 
Upvote 0
You are receiving that message because the data in the target worksheet (in my example it's Sheet1) hasn't been filtered. If you first filter the data, and then run the code, it should copy the last filtered data to the next available row, which is what I understood you wanted. Is this not the case? Or did you want to copy/paste regardless of whether the data is filtered?
 
Upvote 0
You are receiving that message because the data in the target worksheet (in my example it's Sheet1) hasn't been filtered. If you first filter the data, and then run the code, it should copy the last filtered data to the next available row, which is what I understood you wanted. Is this not the case? Or did you want to copy/paste regardless of whether the data is filtered?
Correct, I'd prefer to copy/paste regardless of whether the data is filtered. Thank you for your assistance.
 
Upvote 0
Okay, in that case, try the following code instead, which will copy/paste regardless of whether the data is filtered...

VBA Code:
Option Explicit

Sub CopyLastRow()

    Dim targetSheet As Worksheet
    Dim lastColumn As Long
    Dim lastRow As Long
    Dim nextRow As Long
    Dim copyFromRange As Range
    
    Set targetSheet = ThisWorkbook.Worksheets("Sheet1")
    
    With targetSheet
        lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    If lastRow = 1 Then
        MsgBox "No records found.", vbExclamation
        Exit Sub
    End If
    
    With targetSheet
        Set copyFromRange = .Range(.Cells(lastRow, "A"), .Cells(lastRow, lastColumn))
    End With
    
    With targetSheet
        If .FilterMode Then .ShowAllData
    End With
    
    With targetSheet
        nextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With
    
    copyFromRange.Copy targetSheet.Cells(nextRow, "A")
    
End Sub

Hope this helps!
 
Upvote 0
Solution
Okay, in that case, try the following code instead, which will copy/paste regardless of whether the data is filtered...

VBA Code:
Option Explicit

Sub CopyLastRow()

    Dim targetSheet As Worksheet
    Dim lastColumn As Long
    Dim lastRow As Long
    Dim nextRow As Long
    Dim copyFromRange As Range
   
    Set targetSheet = ThisWorkbook.Worksheets("Sheet1")
   
    With targetSheet
        lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
   
    If lastRow = 1 Then
        MsgBox "No records found.", vbExclamation
        Exit Sub
    End If
   
    With targetSheet
        Set copyFromRange = .Range(.Cells(lastRow, "A"), .Cells(lastRow, lastColumn))
    End With
   
    With targetSheet
        If .FilterMode Then .ShowAllData
    End With
   
    With targetSheet
        nextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With
   
    copyFromRange.Copy targetSheet.Cells(nextRow, "A")
   
End Sub

Hope this helps!
Good morning, this does work, but because I have signature lines within the range it will copy those as being the last row of data. Is there a way to indicate in the code to copy data up to row 25? Thank you!

1733758269614.png
 
Upvote 0
So, if I understood you correctly, the last row to be copied will occur between Row 3 and Row 25, inclusive, with filtered and non-filtered data, correct?

If so, does the last row within that range always get copied to Row 26?
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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