什么是更有效的vba移动在下一个树代码行

你好,我有一个问题,在VBA中,下一个代码树是什么效率更高

选项1:

While fin if activecell.value = "Comp" ' do something ' I use some many time the value of the activecell or the line ' im actually end if activecell.offset(1,0).activate loop 

选项2:

 dim i as long i=0 While fin if activecell.offset(i,0).value = "Comp" ' do something ' I use some many time the value of the activecell or the line ' im actually end if i = i + 1 loop 

选项3:'因为我用了很多次我不知道的实际行'如果最好把这个值转换成一个variables

 dim i as long dim x as string i=0 While fin x = activecell.offset(i,0) if x = "Comp" ' do something ' I use some many time the value of the activecell or 'the line im actually end if i = i + 1 loop 

在此先感谢您的帮助

PD FOr所有的代码我有

 Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Application.EnableEvents = False ActiveSheet.DisplayPageBreaks = False Application.DisplayAlerts = False 

选项4,也就是你没有写的选项4比选项1-3更有效率。 不要打扰激活或抵消任何细胞。 只需将您的数据加载到一个Variant数组,然后对其进行操作。

 Dim v As Variant Dim i As Long v = Sheet1.Range("A1:A1000").Value ' or wherever your data is For i = 1 To UBound(v, 1) If v(i, 1) = "Comp" Then ' do something End If Next i 

选项2。

使用选项1,您可以在每次迭代中激活单元格,这是不必要的。 选项3您设置为一个variables,但只使用一次,所以在设置中有成本,但在初始检查之外没有用处。

此外,当检查使用Value2,因为这不检查货币/date和string将比当前使用的隐式.Value更快:

 if activecell.offset(i,0).Value2 = "Comp"