Consulting

Results 1 to 4 of 4

Thread: Copy the Last Row Within a Range and Paste to Last Row in Another Sheet

  1. #1

    Copy the Last Row Within a Range and Paste to Last Row in Another Sheet

    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
    Attached Files Attached Files

  2. #2
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    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

  3. #3
    YES!!! thank you so very much!!!

  4. #4
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    Thanks for the positive feedback, glad having been of some help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •