首页 > 解决方案 > Excel VBA - 将行复制到不同列中的新工作表

问题描述

我正在使用以下 VBA 在特定条件下将行从一张表复制到另一张表。我现在有一个新的 Excel,我想在其中重用它,但是这次我想在 C5 列而不是 A5 列开始粘贴。我知道您可以指定 .cells ,但这并不像我想象的那么简单。

任何帮助都会很棒:)

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

标签: excelvbacopy-paste

解决方案


您不能将整行的副本粘贴到少于整行的目标位置。只复制数据并粘贴到目标的左上角单元格中。

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

推荐阅读