退出嵌套 For 循环 VBA 并重新循环
Exiting Nested For loop VBA and re-looping
我正在尝试使用嵌套的 For 循环。我基本上有 2 个数组,我想使用 array1 中的第一个变量和 array2 中的第一个变量来执行一些操作,依此类推,直到数组耗尽。不幸的是,Exit For 不会退出 For 级别。因此,我尝试使用 goTo 命令,但随后我清楚地收到 "This array is fixed or temporarily locked" 错误,因为我正在尝试重新访问该数组。我不知道如何在 VBA 中解决这个问题。下面是我的代码,在 MsgBox 中会发生一些操作(需要成对的 (dAFL,AFL),(dSF,SF) 等):
For Each vN In Array(dAFLcell, dSFcell, dSOcell, dFIGcell, dIBAcell, dIBXcell)
a = 0
For Each vN2 In Array(AFLcell, SFcell, SOcell, FIGcell, IBAcell, IBXcell)
If i = a Then
MsgBox a
GoTo end_of_for
End If
a = a + 1
Next vN2
end_of_for:
i = i + 1
Next vN
您可以使用布尔标志 - 我不知道这是公认的方法,但我不时使用它。
Dim skipBool as Boolean = False
For Each vN In Array(dAFLcell, dSFcell, dSOcell, dFIGcell, dIBAcell, dIBXcell)
a = 0 'I think you want this out here, otherwise a will always equal 0
For Each vN2 In Array(AFLcell, SFcell, SOcell, FIGcell, IBAcell, IBXcell)
If Not skipBool Then 'run this stuff only if we don't want to skip it (duh!)
If i = a Then
MsgBox a
skipBool = True 'set skipBool to be True (we want to skip it!)
End If
a = a + 1
End If
Next vN2
i = i + 1
skipBool = False 'reset skipBool for the next go around
Next vN
我确信这段代码可以进一步优化(老实说,我还没有测试过),但看起来这就是你想要的。
老实说,唯一的问题可能是 a = 0
在第二个 for 循环中,这就是您没有得到预期结果的原因。自从我使用 VBA 以来已经有一段时间了(我只使用过 VB.NET),所以我不记得那里的确切语法。我会尝试解决这个问题,然后返回方法的出口。如果它仍然不起作用,我的代码应该。
这是另一种可能的方法:
Dim vn, Vn2 As Variant
Dim i, min As Integer
vn = Array(dAFLcell, dSFcell, dSOcell, dFIGcell, dIBAcell, dIBXcell)
Vn2 = Array(AFLcell, SFcell, SOcell, FIGcell, IBAcell, IBXcell)
If UBound(vn) <= UBound(Vn2) Then
min = UBound(vn)
Else
min = UBound(Vn2)
End If
For i = LBound(vn) To min
If vn(i) = Vn2(i) Then
MsgBox vn(i)
Exit For
End If
Next i
我正在尝试使用嵌套的 For 循环。我基本上有 2 个数组,我想使用 array1 中的第一个变量和 array2 中的第一个变量来执行一些操作,依此类推,直到数组耗尽。不幸的是,Exit For 不会退出 For 级别。因此,我尝试使用 goTo 命令,但随后我清楚地收到 "This array is fixed or temporarily locked" 错误,因为我正在尝试重新访问该数组。我不知道如何在 VBA 中解决这个问题。下面是我的代码,在 MsgBox 中会发生一些操作(需要成对的 (dAFL,AFL),(dSF,SF) 等):
For Each vN In Array(dAFLcell, dSFcell, dSOcell, dFIGcell, dIBAcell, dIBXcell)
a = 0
For Each vN2 In Array(AFLcell, SFcell, SOcell, FIGcell, IBAcell, IBXcell)
If i = a Then
MsgBox a
GoTo end_of_for
End If
a = a + 1
Next vN2
end_of_for:
i = i + 1
Next vN
您可以使用布尔标志 - 我不知道这是公认的方法,但我不时使用它。
Dim skipBool as Boolean = False
For Each vN In Array(dAFLcell, dSFcell, dSOcell, dFIGcell, dIBAcell, dIBXcell)
a = 0 'I think you want this out here, otherwise a will always equal 0
For Each vN2 In Array(AFLcell, SFcell, SOcell, FIGcell, IBAcell, IBXcell)
If Not skipBool Then 'run this stuff only if we don't want to skip it (duh!)
If i = a Then
MsgBox a
skipBool = True 'set skipBool to be True (we want to skip it!)
End If
a = a + 1
End If
Next vN2
i = i + 1
skipBool = False 'reset skipBool for the next go around
Next vN
我确信这段代码可以进一步优化(老实说,我还没有测试过),但看起来这就是你想要的。
老实说,唯一的问题可能是 a = 0
在第二个 for 循环中,这就是您没有得到预期结果的原因。自从我使用 VBA 以来已经有一段时间了(我只使用过 VB.NET),所以我不记得那里的确切语法。我会尝试解决这个问题,然后返回方法的出口。如果它仍然不起作用,我的代码应该。
这是另一种可能的方法:
Dim vn, Vn2 As Variant
Dim i, min As Integer
vn = Array(dAFLcell, dSFcell, dSOcell, dFIGcell, dIBAcell, dIBXcell)
Vn2 = Array(AFLcell, SFcell, SOcell, FIGcell, IBAcell, IBXcell)
If UBound(vn) <= UBound(Vn2) Then
min = UBound(vn)
Else
min = UBound(Vn2)
End If
For i = LBound(vn) To min
If vn(i) = Vn2(i) Then
MsgBox vn(i)
Exit For
End If
Next i