PDA

View Full Version : [SOLVED:] Copy the Last Row Within a Range and Paste to Last Row in Another Sheet



rorobear
01-10-2023, 10:04 AM
Hello Everyone,

If someone could assist with this little macro I’d really appreciate it. I have a workbook with to sheets, InvoiceSheet and DataSheet. My code, when the ADD TO RECORD button is clicked, copys the very last row on the InvoiceSheet to the next empty row in the DataSheet, but that’s not what I want. I want to copy the last in the InvoiceSheet starting from row B18. I’ve attached the Workbook. Any assistance is greatly appreciated.


Option Explicit

Sub RecordofInvoice()

Dim lastrowSrc As Long
Dim lastrowDest As Long

'Get last row of data
lastrowSrc = Sheets("InvoiceSheet").Range("B" & Rows.Count).End(xlUp).Row

'Get first blank row (last row of data +1)
lastrowDest = Sheets("DataSheet").Range("B" & Rows.Count).End(xlUp).Row + 1

'Copy row
Sheets("InvoiceSheet").Range("B" & lastrowSrc).EntireRow.Copy Sheets("DataSheet").Range("A" & lastrowDest)

End Sub

rollis13
01-10-2023, 11:45 AM
Is this what you are looking for ?
Option Explicit
Sub RecordofInvoice()
Dim lastrowSrc As Long
Dim lastrowDest As Long
'Get last row of data
lastrowSrc = Sheets("InvoiceSheet").Range("B37").End(xlUp).Row '<- changed
'check if no data
If lastrowSrc = 18 Then '<- added this If/Then/End
MsgBox "Nothing to copy"
Exit Sub
End If
'Get first blank row (last row of data +1)
lastrowDest = Sheets("DataSheet").Range("B" & Rows.Count).End(xlUp).Row + 1
'Copy rows
Sheets("InvoiceSheet").Range("B19:B" & lastrowSrc).EntireRow.Copy Sheets("DataSheet").Range("A" & lastrowDest) '<- changed
End Sub

rorobear
01-10-2023, 01:23 PM
YES!!! thank you so very much!!!

rollis13
01-10-2023, 03:48 PM
Thanks for the positive feedback, glad having been of some help.