简体   繁体   中英

How to Copy/Paste to next empty row?

I have code to copy a Worksheet A, columns A:C (no set row quantities, this will be re-used and the quantities will change) and paste to the first blank row in the same workbook, different sheet, Worksheet B (this also has no set row quantities and will change).

Worksheet B has a formula in the same columns that I want to paste to that returns "" if there is no data. I think VBA is seeing the "" and assuming there is data there; however, it is not pasting even to lines without said formula.

Sub Copy_Created_Fringe_Accounts()
    Dim SourceRange As Range
    Dim DestRange As Range
    Dim DestSheet As Worksheet
    Dim LastRow As Long

'Source sheet and range
    Set SourceRange = Sheets("CREATED FRINGE ACCTS").Range("A2:C500")

'Destination sheet and range
    Set DestSheet = Sheets("99 BUDGET WORKSHEET")

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

'Copy and paste to destination
    Set DestRange = DestSheet.Range("A" & LastRow + 1)
    SourceRange.Copy DestRange

End Sub

Nothing happens when I run it. I expect to see the data from Worksheet A copied to Worksheet B, starting at the first available empty row.

I am fairly new to VBA so any help/understanding is appreciated.

Finding the last row

Try using UsedRange to find the last used row as this is safer than using Find .

LastRow = DestSheet.UsedRange.Rows.Count


A side note

If your code resides in the same place as these worksheets then I would recommend using their code name . This will protect you from running into an error if the sheet doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM