我想複製一行到另一個工作簿(只有當有匹配時),我可以用一個簡單的循環完成,但我想用一些更好和可能較快的方法:VBA excel行復制方法不起作用
Set wbk = Workbooks.Open(FROM)
Set wskz = wbk.Worksheets("Sheet1")
Set wbi = Workbooks.Open(TO)
Set wski = wbi.Worksheets("Sheet1")
si = 5
Do While wski.Cells(si, 1).Text <> "END" ' loop through the values in column "A" in the "TO" workbook
varver = wski.Cells(si, 1).Text ' data to look up
s = 5
Do While wskz.Cells(s, 1).Text <> "END" ' table where we search for the data in the "FROM" workbook
If wskz.Cells(s, 1).Text = varver Then Exit Do
s = s + 1
Loop
If wskz.Cells(s, 1).Text <> "END" Then
' I am trying this copy method to replace the loop but it throws an error
wskz.Range(Cells(s, 1), Cells(s, 250)).Copy Destination:=wski.Range(Cells(si, 1), Cells(si, 250))
' this is the working loop:
'For i = 1 To 250
' wskz.Cells(s, i) = wski.Cells(si, i)
' i = i + 1
'End If
'Next i
與新的複製方法的問題拋出,因爲它可以被上面看到一個錯誤。
非常感謝您的幫助!
由於在答案中提到,其原因很可能是因爲您同時設置了範圍的工作,你沒有告訴它該工作表中的'.Cells()'上。只需在'.cells()'之前分別添加'wskz.'和'wski.'。 – BruceWayne