嵌套直到工作仅1次

时间:2019-01-05 09:56:48

标签: excel vba

总共应该有125个输出,但是只有5个输出会出现,因为只有最里面的循环(c循环)运行了5次。循环a和b没有运行,我不确定为什么。我也尝试使用“ for .... next”来解决类似问题,它给出了45个输出。

请求您的帮助。对不起,英语不好。

Sub ColourCheck2()

Dim a, b, c As Long

Application.ScreenUpdating = False

a = 1
b = 6
c = 11

Do Until a > 5
    Do Until b > 10
        Do Until c > 15
            Sheet2.Cells(b + c, a).Value = a & " " & b & " " & c
            Sheet2.Cells(b + c, a).Interior.Color = RGB(a, b, c)
            c = c + 1
        Loop
    b = b + 1
    Loop
    a = a + 1
Loop

Application.ScreenUpdating = True

End Sub

结果


A列B列C列
1 6 11 2 6 11 3 6 11
1 6 12 2 6 12 3 6 12
1 6 13 2 6 13 3 6 13
1 6 14 2 6 14 3 6 14
1 6 15 2 6 15 3 6 15
1 7 11 2 7 11 3 7 11
1 7 12 2 7 12 3 7 7
1 7 13 2 7 13 3 7 13
1 7 14 2 7 14 3 7 14
1 7 15 2 7 15 3 7 15
1 8 11 2 8 11 3 8 11
1 8 12 2 8 12 3 8 12
1 8 13 2 8 13 3 8 13
1 8 14 2 8 14 3 8 14
1 8 15 2 8 15 3 8 15
等等,下面也向下。

对于D和E列也是如此

2 个答案:

答案 0 :(得分:1)

你在这里

Sub ColourCheck2()

    Dim a As Long, b As Long, c As Long, d As Long

    Application.ScreenUpdating = False

    a = 1
    b = 6
    c = 11
    d = 0

    Do Until a > 5
        d = 0
        b = 6
        Do Until b > 10
            c = 11
            Do Until c > 15
                Sheet2.Cells(b + (d * 4) + c, a).Value = a & " " & b & " " & c
                Sheet2.Cells(b + (d * 4) + c, a).Interior.Color = vbYellow ' RGB(a, b, c)
                c = c + 1

            Loop
            d = d + 1
            b = b + 1
        Loop
        a = a + 1
    Loop

    Application.ScreenUpdating = True

End Sub

enter image description here

答案 1 :(得分:1)

或仅用于/下一个循环:

For a = 1 to 5
    For b = 6 to 10
        For c = 11 to 15
            Sheet2.Cells(b + c, a).Value = a & " " & b & " " & c
            Sheet2.Cells(b + c, a).Interior.Color = RGB(a, b, c)
        Next c
    Next b
Next a