[英]vba copy range to cell in new worksheet
I have two sheets I am working with, within the same workbook. 我在同一工作簿中有两张正在处理的工作表。 For each one I defined the variables
db
and rs
. 我为每个变量定义了
db
和rs
。
The goal here is to copy a certain row (given it matches my conditions) from db
to the next empty row on rs
. 此处的目标是将
db
的某一行(假设它符合我的条件)复制到rs
上的下一个空行 。
I've tried: 我试过了:
db.Range(db.Cells(i, 1), db.Cells(i, M)).Copy _ Destination:=rs.Cells(N, 1).End(xlUp).Offset(1, 0)
and 和
Set resrow = db.Range(db.Cells(i, 1), db.Cells(i, M)) 'next line is completely wrong inside the Range bracket rs.Range(rs.Cells(N, 1).End(xlUp).Offset(1, 0)).Resize(1, M).Value = resrow.Value
Note that i
and M
are integers
. 请注意,
i
和M
是integers
。
I appears to me that I am trying to set a range value to a cell. 我似乎在尝试为单元格设置范围值。 How can I best fix it?
我怎样才能最好地解决它?
Thanks in advance 提前致谢
I think you are mixing ranges with cells, I'd try something like: 我认为您正在混合范围与单元格,我会尝试类似:
rsLastRow = rs.UsedRange.Rows.Count
db.Range(Cells(i, 1), Cells(i, M)).Copy
rs.Range("A" & rsLastRow).PasteSpecial Paste:=xlPasteValues
Avoid Copy & Paste to reduce memory usage and improve the VBA performance 避免复制和粘贴以减少内存使用并提高VBA性能
Set rng1 = db.Cells(Rows.Count, "A").End(xlUp)
Set rn2 = db.UsedRange
col = rn2.Columns.Count
For Each cel In db.Range("A2:A" & rng1.Row)
if (condition) then
Set rng2 = rs.Cells(Rows.Count, "A").End(xlUp)
rs.Range(Cells(rng2.row, 1), Cells(rng2.row, col)).Value = db.Range(cells(cel.row,1),Cells(cel.row,col)).Value
end if
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.