简体   繁体   English

如何复制/粘贴到下一个空行?

[英]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). 我有代码复制工作表A的A:C列(未设置行数,将重新使用该行,并且数量将更改),然后粘贴到同一工作簿,不同工作表B的同一行的第一空白行(此也没有设置行数,并且会更改)。

Worksheet B has a formula in the same columns that I want to paste to that returns "" if there is no data. 工作表B在要粘贴到的同一列中有一个公式,如果没有数据,则返回“”。 I think VBA is seeing the "" and assuming there is data there; 我认为VBA正在看到“”,并假设那里有数据。 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. 我希望从第一个可用的空行开始,将工作表A中的数据复制到工作表B中。

I am fairly new to VBA so any help/understanding is appreciated. 我对VBA还是很陌生,因此感谢您的帮助/理解。

Finding the last row 找到最后一行

Try using UsedRange to find the last used row as this is safer than using Find . 尝试使用UsedRange查找最后使用的行,因为这比使用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. 如果工作表不存在,这可以防止您遇到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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