Excel VBA - Copy Rows to new Sheet in different column

末鹿安然 提交于 2020-01-25 06:22:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!