双“ for”循环-仅内部循环有效

se7en94

我的第一篇文章在这里。你们帮助了我上百万次,但是这次我没有设法在Google或这里找到答案。

我在Excel中创建了2个for循环,一个在另一个内,在这里缩短了版本:

For r = 3 To 25
    For col = rota_current_col To 100
        Debug.Print "Current position:" & r & "," & col     // + some code later
    Next col
//some code
Next r

而且第一个循环根本不起作用。我没有在循环内的代码中触摸任何这些值(r,col)。此调试打印显示的值为从3.7到3,100的值,但没有循环到“ r”值。

我希望这很清楚,在此先感谢!

编辑1:按要求的完整循环:

For r = 3 To 25 ' NOT WORKING :(

    For col = rota_current_col To 100
        Debug.Print "Current position:" & r & "," & col & " current Rota position: " & rota_current_row & "," & rota_current_col & " current Comp position: " & comp_current_row & "," & comp_current_col

        Select Case Cells(rota_current_row, rota_current_col)
        Case "U", "UZ", "U1", "UZ1"
            If Cells(rota_current_row, rota_current_col) <> Cells(comp_current_row, comp_current_col) Then
                result.Cells(current, 1) = rota.Cells(rota_current_row, 1)
                result.Cells(current, 2) = rota.Cells(rota_current_row, 2)
                result.Cells(current, 3) = rota.Cells(rota_current_row, 3)
                result.Cells(current, 4) = rota.Cells(rota_current_row, rota_current_col)
                result.Cells(current, 5) = rota.Cells(rota_current_row, rota_current_col).Address
                result.Cells(current, 6) = comp.Cells(comp_current_row, comp_current_col)
                result.Cells(current, 7) = comp.Cells(comp_current_row, comp_current_col).Address
                current = current + 1
            End If
        End Select

        rota_current_col = rota_current_col + 1
        comp_current_col = comp_current_col + 1

    Next col

rota_current_row = rota_current_row + 1
comp_current_row = comp_current_row + 1

Next r

您要我粘贴完整的代码吗?

伊森

在col = 100之前,r不会变为4。内循环需要完成,然后外循环开始。

尝试这个

Sub yo()
r = 3
For col = rota_current_col To 100
    Debug.Print "Current position:" & r & "," & col
    r = r + 1
    If r = 26 Then Exit Sub
Next col
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章