• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Problem in 3 level Nested Loop

hey Hii. I was trying to learn nested loop, but i encountered a problem. I have put 3 level loops. As per my understanding loop shall run 5X5X5=125 times. But it is running only 45 times.
I am not able to figure out where i am wrong. Below is the code. I tried many things but its not working. pls help. thanks a lot. Sheet attached for reference.

Code:
Sub ColourCheck2()

Dim a, b, c As Integer

Application.ScreenUpdating = False

For a = 0 To 4
    For b = 0 To 4
        For c = 0 To 4
            Sheet2.Cells(c + b + 1, a + 1).Value = a * 20 & " " & b * 20 & " " & c * 20
            Sheet2.Cells(c + b + 1, a + 1).Interior.Color = RGB(a * 20, b * 20, c * 20)
        Next
    Next
Next

Application.ScreenUpdating = True

End Sub

thanks........
 

Attachments

  • Nested loop Test.xlsm
    17.2 KB · Views: 2
Last edited by a moderator:
Code:
Sub ColourCheck2()

Dim a as integer, b as integer, c As Integer

Application.ScreenUpdating = False

For a = 0 To 4
   For b = 0 To 4
       For c = 0 To 4
            Sheet2.Cells(a*25 + b*5 + c,  1).Value = a * 20 & " " & b * 20 & " " & c * 20
            Sheet2.Cells(a*25 + b*5 + c,  1).Interior.Color = RGB(a * 20, b * 20, c * 20)
       Next
   Next
Next

Application.ScreenUpdating = True

End Sub
 
rubikscube991
Do You try to see 3D with 2D?
If so then You gotta select which of those (a,b,c) would present that 3rd D.
eg 7*c + instead of c +
Then You'll see 3* 5*5 ...
( ... and same thing, with those colors )

... or as previous reply ;)
 
Code:
Sub ColourCheck2()
Dim a as integer, b as integer, c As Integer

Application.ScreenUpdating = False

For a = 0 To 4
   For b = 0 To 4
       For c = 0 To 4
            Sheet2.Cells(a*25 + b*5 + c,  1).Value = a * 20 & " " & b * 20 & " " & c * 20
            Sheet2.Cells(a*25 + b*5 + c,  1).Interior.Color = RGB(a * 20, b * 20, c * 20)
       Next
   Next
Next

Application.ScreenUpdating = True

End Sub

No Hui sorry but there was some error in your code. But after correction also it was not working propoerly. anyway I made few changes and then it solved. thanks for all your help. you helped a lot.. I will paste the code for someone's reference.

Code:
Sub ColourCheck21()

Dim a, b, c, k As Integer

Application.ScreenUpdating = False

For a = 1 To 5
b = Empty
c = Empty
    For b = 1 To 5
    c = Empty
    k = Sheet2.Cells(32000, a).End(xlUp).Row
        For c = 1 To 5
            Sheet2.Cells(c + k, a).Value = a & " " & b & " " & c
            'Sheet1.Cells(c + k, a).Font.Color = RGB(a * 10, b * 10, c * 10)
            Sheet2.Cells(c + k, a).Interior.Color = RGB(a, b, c)
        Next c
    Next b
Next a

Application.ScreenUpdating = True

End Sub
 
Last edited by a moderator:
Back
Top