问题
I am using the following VBA to copy under a certain condition the Rows from one Sheet to another. I've now a new Excel where I want to reuse this, however this time I would like to start the pasting at column C5 rather than A5. I know that you can specify .cells but this is not working here that simple as I had imagined.
Any help would be great :)
Sub CopyRows()
Dim Zeile As Long
Dim ZeileMax As Long
Dim n As Long
Set RAW = Worksheets("RAWdata")
Set Closed = Worksheets("RAWclosed")
With RAW
ZeileMax = .UsedRange.Rows.Count
n = 5
For Zeile = 2 To ZeileMax
If .Cells(Zeile, 5).Value = "Closed" Then
.Rows(Zeile).Copy Destination:=Closed.Rows(n)
n = n + 1
End If
Next Zeile
End With
End Sub
回答1:
You cannot paste a full row's copy into a destination that is less than a full row. Only copy what is data and paste into the top-left cell of the destination.
For Zeile = 2 To ZeileMax
If .Cells(Zeile, 5).Value = "Closed" Then
.Range(.cells(Zeile, "A"), .cells(Zeile, .columns.count).end(xltoleft)).Copy _
Destination:=Closed.cells(n, "C")
n = n + 1
End If
Next Zeile
来源:https://stackoverflow.com/questions/51892211/excel-vba-copy-rows-to-new-sheet-in-different-column