问题
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