我正在尝试将数据从Q3表1粘贴到Q3表2.每个数据应粘贴在Q3表2的最后一个数据下面一行(从单元格A4开始)。不幸的是,行
Worksheets("Q3 Sheet 2").Range("A3").End(xlUp).Offset(1, 0) = .Offset(iRow, 0)
不这样做。相反,它会粘贴A4中的所有数据,并且它们会继续相互覆盖,因此A4中只有一个条目,从A4到A14应该有多个条目。请帮忙。谢谢!
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
答案 0 :(得分:1)
只需要进行两项小改动。
Worksheets("Q3 Sheet 2").Range("A3").End(xlUp).Offset(1, 0) = .Offset(iRow, 0)
应该阅读
Worksheets("Q3 Sheet 2").Range("A2").End(xlDown).Offset(1, 0) = .Offset(iRow, 0)
答案 1 :(得分:1)
我已经重写了代码以使用范围(而不是使用范围来获取行然后循环行数),维度变量和屏幕更新关闭(速度),加上它比查找更健壮找到最后一条记录时
如果拥有的金额超过1000,此版本会将整行从Q3表1复制到Q3表2.它可以减少到您想要的任何数量的单元格(我想您可能需要两个单元格) ?)
[pdate:进一步整理代码,添加ws2
变量,删除AmountOwned
和冗余nCustomers
]
Sub Update()
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range
Application.ScreenUpdating = False
Set ws = Worksheets("Q3 Sheet 1")
Set ws2 = Worksheets("Q3 Sheet 2")
Set rng1 = ws.Range(ws.[a4], ws.Cells(Rows.Count, "A").End(xlUp))
For Each rng2 In rng1
'If amount owed > 1000 then transfer customer ID and amount owing to Q3 Sheet 2
If rng2.Offset(0, 1) - rng2.Offset(0, 2) > 1000 Then rng2.EntireRow.Copy ws2.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
Next
Application.ScreenUpdating = True
End Sub
答案 2 :(得分:0)
将此行更改为
Worksheets("Q3 Sheet 2").Range("A3").End(xlDown).Offset(1, 0) = .Offset(iRow, 0)
[]的
答案 3 :(得分:0)
Worksheets("Q3 Sheet 2").cells(rows.count,1).End(xlUp).Offset(1, 0) = .Offset(iRow, 0)
假设A列中的工作表没有数据下降