Ich versuche, eine Zeile in eine andere Arbeitsmappe zu kopieren (nur wenn es eine Übereinstimmung gibt), und ich kann das mit einer einfachen Schleife erreichen, aber ich möchte etwas besser und möglicherweise verwenden schnellere Methode:VBA Excel Zeilenkopiermethode funktioniert nicht
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
Das Problem mit dem neuen Kopierverfahren wirft einen Fehler, wie er oben zu sehen ist.
Vielen Dank im Voraus für Ihre Hilfe!
Wie in den Antworten erwähnt, ist der Grund, wahrscheinlich, weil während Sie das Arbeitsblatt für den Bereich eingestellt, man es nicht sagen, welches Arbeitsblatt die '.Cells()' sind. Fügen Sie einfach 'wskz.' und' wski. vor '.cells()' hinzu. – BruceWayne