Excel VBA中嵌套循环中无错误的下一步
我试图找出一种方法,通过检查“新数据”文件中的表,在“系统文件”中的单元格上运行Vlookup。但是,如果存在#N/A错误,我希望单元格的值保持不变。我已经想出了以下几点,但是,我一直得到一个“下一个没有为”的错误。是否可以转义嵌套的For Next循环 tl;dr语义版本:Excel VBA中嵌套循环中无错误的下一步,vba,excel,Vba,Excel,我试图找出一种方法,通过检查“新数据”文件中的表,在“系统文件”中的单元格上运行Vlookup。但是,如果存在#N/A错误,我希望单元格的值保持不变。我已经想出了以下几点,但是,我一直得到一个“下一个没有为”的错误。是否可以转义嵌套的For Next循环 tl;dr语义版本: For i 1 to 10 For j 1 to 3 Something with .Cells(i,j) Set range X = .Find(thing
For i 1 to 10
For j 1 to 3
Something with .Cells(i,j)
Set range X = .Find(thing
If X = Nothing Then
Next j *** <THIS IS WHERE MY ERROR IS THROWN
Else
-Do Something with X-
End if
Next j
Next i
当然,***们实际上并不在那里。有没有想过我该如何做到这一点?
提前感谢使用
For i=1 to 10
For j=1 to 3
Something with .Cells(i,j)
Set range X = .Find(thing
If X = Nothing Then
Goto Nextj *** <THIS IS WHERE MY ERROR IS THROWN
Else
-Do Something with X-
End if
NextJ:
Next j
Next i
用于i=1到10
对于j=1到3
带有.Cells(i,j)的东西
设置范围X=.Find(对象)
如果X=无,则
Goto Nextj***Exit For提前终止当前For循环(在您的案例中是内部循环)。他不想终止For循环,他想跳到下一个迭代。我认为使用“Goto”被认为是不好的做法语句?同时,这是一种不同的方法,允许我思考的逻辑流。对我来说,良好的实践是有效的:)在这种情况下会很有效。这种方法肯定有效,所以非常感谢!我想我只是觉得测试Nothing=True的逻辑流是不雅观的。
For i=1 to 10
For j=1 to 3
Something with .Cells(i,j)
Set range X = .Find(thing
If X = Nothing Then
Goto Nextj *** <THIS IS WHERE MY ERROR IS THROWN
Else
-Do Something with X-
End if
NextJ:
Next j
Next i
For i = 1 to 10
For j = 1 to 3
Something with .Cells(i,j)
Set rngX = .Find(thing)
If Not rngX Is Nothing Then
Set rngY = .Find(thingelse)
If Not rngY Is Nothing Then
'something with rngX and rngY
End If
End if
Next j
Next i