Copy and Paste in First Blank Row Loop

最后都变了- 提交于 2019-12-25 06:58:08

问题


In Sheet 1 I have data in columns. In worksheet 2 I have data that is designated specifically for copying and pasting into the blank rows of Sheet 1. I'm trying to loop the data in Sheet 2 and copy it into Sheet 1 in the following rows (24,25,26,etc.) I'm having trouble with the pasting part. I also need the code to be uniform so that I can modify sheet 2 and run the macro without changing anything. I'm using the code below.

Dim LastRow2 As Long
Dim BlankRow As Long
BlankRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row + 1
LastRow2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
For i = BlankRow To 50
    For j = 2 To LastRow2
        Sheets("Sheet2").Range("A" & j).Copy
        Sheets("Sheet1").Range("A" & i).PasteSpecial
    Next j
Next i

I've placed a very simplified example below as well just to help give an idea of what exactly I'm trying to do. The rows in Sheet 1 are in the thousands in my spreadsheet, and Sheet 2 can get to be around 30. So it's important to me that I loop it using the first blank row in Sheet 1 as the starting point.

Sheet 1
        A
1       x
2       x
3       x
4       x
5       x
6       x


Sheet 2
        A
1       Headings
2       Tyler
3       Bill
4       Rob
5       Dennis

回答1:


You don't need a nested loop. You just need to 1. Something like:

Dim LastRow2 As Long, BlankRow As Long, j As Long
BlankRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row + 1
LastRow2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
For j = 2 To LastRow2
    Sheets("Sheet2").Range("A" & j).Copy
    Sheets("Sheet1").Range("A" & BlankRow).PasteSpecial xlPasteValues
    BlankRow = BlankRow + 1
Next j
Application.CutCopyMode = False

Is this what you're trying? HTH.



来源:https://stackoverflow.com/questions/28202581/copy-and-paste-in-first-blank-row-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!