偏移和结束功能
我正在尝试将数据从 Q3 表 1 粘贴到 Q3 表 2。每条数据应粘贴在 Q3 表 2 上最后一条数据下方的一行(从单元格 A4 开始)。不幸的是,该行
Worksheets("Q3 Sheet 2").Range("A3").End(xlUp).Offset(1, 0) = .Offset(iRow, 0)
不这样做。相反,它将所有数据粘贴到 A4 中,并且它们继续相互覆盖,因此,当从 A4 一直到 A14 应该有多个条目时,A4 中只有一个条目。请帮忙。谢谢!
With Worksheets("Q3 Sheet 1").Range("A3")
'Count total number of entries
nCustomers = Range(.Offset(1, 0), .Offset(1, 0).End(xlDown)).Rows.Count
'Loop through all entries looking for amounts owed > 1000
For iRow = 1 To nCustomers
AmountOwed = .Offset(iRow, 1) - .Offset(iRow, 2)
'If amount owed > 1000 then transfer customer ID and amount owing to Q3 Sheet 2
If AmountOwed > 1000 Then
Worksheets("Q3 Sheet 2").Range("A3").End(xlUp).Offset(1, 0) = .Offset(iRow, 0)
End If
Next iRow
End With
I am trying to paste data from Q3 Sheet 1 to Q3 Sheet 2. Each piece of data should be pasted one row below the last piece of data on Q3 Sheet 2 (starting in cell A4). Unfortunately, the line
Worksheets("Q3 Sheet 2").Range("A3").End(xlUp).Offset(1, 0) = .Offset(iRow, 0)
does not do this. Instead it pastes all the data in A4 and they continue to overwrite each other, so that there is only one entry in A4 when there should be multiple entries from A4 all the way up to A14. Please help. Thanks!
With Worksheets("Q3 Sheet 1").Range("A3")
'Count total number of entries
nCustomers = Range(.Offset(1, 0), .Offset(1, 0).End(xlDown)).Rows.Count
'Loop through all entries looking for amounts owed > 1000
For iRow = 1 To nCustomers
AmountOwed = .Offset(iRow, 1) - .Offset(iRow, 2)
'If amount owed > 1000 then transfer customer ID and amount owing to Q3 Sheet 2
If AmountOwed > 1000 Then
Worksheets("Q3 Sheet 2").Range("A3").End(xlUp).Offset(1, 0) = .Offset(iRow, 0)
End If
Next iRow
End With
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需要两个小改动。
应该读
Only two small changes are needed.
should read
我重写了代码以使用范围(而不是使用范围来获取行然后循环行号),对变量进行标注并关闭屏幕更新(以提高速度),而且在查找时向上查找比向下查找更稳健最后记录
如果拥有的数量超过 1000,此版本会将整行从 Q3 表 1 复制到 Q3 表 2。它可以削减到您想要的任何数量的单元格(我认为您可能想要两个单元格? )
[更新:进一步整理代码,添加了
ws2
变量,删除了AmountOwned
和冗余的nCustomers
]I've rewritten the code to work with ranges (rather than use a range to get rows then loop row numbers), dimension the variables and with screenupdating off (for speed), plus it is more robust to look up than down when finding the last record
This version copies the entire row from Q3 Sheet 1 to Q3 Sheet 2 if amount owned exceeds 1000. It can be cut back to whatever amount of cells your want (I think you may want two cells?)
[pdate: Tidied code further, added a
ws2
variable, removedAmountOwned
and redundantnCustomers
]将此行更改为
[] 的
Change this line to
[]'s
假设 A 列中工作表下方没有数据
Assuming there's no data lower down the sheet in column A