瞭解如何創建宏的最佳方法是使用記錄宏功能。它會產生你在工作簿中做什麼的代碼,然而,在這種情況下你需要循環,所以它更復雜。
以下代碼將實現您正在尋找的內容。我在評論中添加了解釋每一行的功能。
Sub copyRow()
Application.ScreenUpdating = False 'Turn off ScreenUpdating so you won't see all the
'actions happen in real time
Dim count As Integer 'Declare variables
Dim lastRow1 As Integer, lastRow2 As Integer
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("Sheet1") 'Set worksheet values
Set ws2 = Worksheets("Sheet2")
ws1.Activate 'Sheet1 needs to be active to perform next step
lastRow1 = ws1.Range("A50").End(xlUp).row 'Identify last row in table to know data size
For i = 2 To lastRow1 'For the number of rows in table, perform the following
count = ws1.Range("F" & i).Value 'Set 'count' variable, number of times to paste row
ws1.Activate 'Sheet2 needs to be active to perform next step
ws1.Range(Range("A" & i), Range("D" & i)).Copy 'Copy data you want to transfer
ws2.Activate
lastRow2 = ws2.Range("A50").End(xlUp).row 'Identify last row in table
lastRow2 = lastRow2 + 1 'Want to paste data to NEXT row
For j = lastRow2 To lastRow2 + count - 1 'Paste the data the number of times indicated
ws2.Range("A" & j).PasteSpecial
Next j
Next i
Application.ScreenUpdating = True 'Turn back on ScreenUpdating to see updated sheet
End Sub
到目前爲止您嘗試過什麼?發佈您的代碼以顯示錯誤所在。 –