[英]How do I copy specific cells from sheet 1 and paste into corresponding rows of sheet 2 , based on values of cells in sheet 2?
[英]How do i copy rows from another sheet and paste them into a sheet that has a table in it?
我正在編寫代碼,我要做的就是從一張工作表中復制數據並將其粘貼到具有表設置的另一張工作表中。
我的代碼完全按照我想要的去做,但是表的大小不會調整為包括所有已復制的行,只有復制數據的第一行才進入表。 其余的則格式化為不在表格中。
這是我運行代碼后的樣子
Sub LastRowInOneColumn()
Dim LastRow As Longenter image description here
Dim i As Long, j As Long
'Find the last used row in a Column
With Worksheets("First Page")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'first row number where you need to paste values in Sheet1'
With Worksheets("Report")
j = .Cells(.Rows.Count, "A").End(xlUp).Row '+ 1
End With
For i = 1 To LastRow
With Worksheets("First Page")
'If .Cells(i, 1).Value = "X" Then
.Rows(i).Copy Destination:=Worksheets("Report").range("A" & j)
j = j + 1
'End If
End With
Next i
End Sub
通常,在表末尾插入將使其自動增長,但在粘貼超出表中列數的范圍時不會自動增長。 有兩種方法可以解決此問題:
1-將復制范圍限制為表格中的列數; 即
.Rows(i).Resize(,4).Copy Destination:=Worksheets("Report").range("A" & j)
' ^^^^^^^^^^^
2-使用方法ListObject.Resize
顯式調整表的大小; 即
With Sheet1.ListObjects(1)
.Resize .Range.Resize(.Range.Rows.count + 1)
End With
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.