excel - 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
解决方案
您不能将整行的副本粘贴到少于整行的目标位置。只复制数据并粘贴到目标的左上角单元格中。
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
推荐阅读
- javascript - RangeError:array.slice 超出了最大调用堆栈大小
- python - 使用 Python/Pandas 在 .csv 中附加一列
- reactjs - 为什么 React/Redux 表单中的输入会将数字变为字符串?
- c# - 如何将身份验证添加到起始页
- python - matplotlib折线图添加子组X轴标签
- c++ - 在 C++ 的单独头文件中完成函数体?
- swift - Apple Watch WCConnectionDelegate,在activationDidComplete 中发送消息偶尔会失败?
- angular - 使用公共 IP 无法访问 AWS EC2 实例中托管的 Angular 6 应用程序
- ruby-on-rails - 在保存在rails框架中之前,如何通过循环去除活动记录中的列值?
- java - 用于实例化列表的变量时,主列表值会发生变化