【发布时间】:2021-10-08 18:56:57
【问题描述】:
我在论坛中发现了许多非常相似的问题,但不知何故没有适合我正在寻找的问题。 我有两个要比较的范围(a 和 b),如果值不匹配,我想将整行复制到预定义的工作表中。目的是查找与先前编辑相比已更改的行/值。
Dim a, b as range
Dim ws1,ws2,ws3 as worksheet
Dim last_row, last_row2 as integer 'assume last_row =15, last_row2=12
Dim i, j, k as integer
last_row=15
last_row2=12
' the orignal range is not massive, but at 500x 6 not small either
Set a=ws1.range("I5:S"& last_row)
Set b=ws2.range("H2:R"& last_row2)
在处理范围中的每个项目时,我看到了不同的方法,但不知道哪种方法最快/最好(循环或每个)。 主要的 if 语句看起来像这样:
'assume i, j are the used as counters running across the range
k = 1
If Not a(i).value=b(j).value then
a(i)EntireRow.copy
ws3.row(k).paste
k = k + 1
end if
解决方案不能基于公式,因为我需要在每次比较后保存 ws3。 非常感谢您对此的任何帮助。谢谢!
【问题讨论】:
-
如果您正在寻找速度,请考虑使用
Variant数组(更多阅读here)。循环遍历每个单元格会很慢。
标签: excel vba for-loop compare range