+ Reply to Thread
Results 1 to 5 of 5

Nested loops problem

Hybrid View

  1. #1
    Registered User
    Join Date
    06-07-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Nested loops problem

    This is my first attempt at using vba. What I'm trying to do is find palindromes that are multiples of three digit numbers. I figured I'd start with 100 and multiply that by 100-999 then jump to 101 times 100-999 and so on untill 999-999 and excel check each number to see if its a palindrome.

    I know my code is ugly but here it is.


    Sub Test2()
        x = 100
        y = 100
        
        Do While x < 1000
    
    'Starts off with x = 100   
    
        A = x * y
    
    'multiplies x times y
    
        b = Str(A)
        c = StrReverse(A)
    
    'converts A to a string and its reverse
    'then compares the two strings  
    
            If StrComp(b, c, vbTextCompare) = 0 Then
            MsgBox b
    
    'tells me if its a palindrome
    
            Else
            End If
            
           
            
    'Heres my problem the code goes through this loop   
    'but never repeats it, y goes to 1000 and stays there.     
            Do While y < 1000
            y = y + 1
            A = x * y
            b = Str(A)
            c = StrReverse(A)
           
            If StrComp(b, c, vbTextCompare) = 0 Then
            MsgBox b
            Else
            End If
           
            Loop
        
        
        x = x + 1
    
    'adds one to x and starts loop over
    
        Loop
        
    End Sub

    I've added some comments but they most likely make the code hard to read.
    While testing this out I tossed in message boxes to see what wasnt working and it appears the my second loop only happens once.




    What I dont want is a solution to how to find the palindromes, what I would like would be if someone could explain why my second loop isnt restarting.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Nested loops problem

    How about ...

    Sub Test3()
        Dim i           As Long
        Dim j           As Long
        Dim ij          As Long
    
        For i = 100 To 999
            For j = i To 999
                ij = i * j
                If CStr(ij) = StrReverse(CStr(ij)) Then
                    Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 3).Value = Array(i, j, ij)
                End If
            Next j
        Next i
    End Sub
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: Nested loops problem

    Hi Symplystyc
    It looks like your y-loop is executing properly
    Try using the Debug statement as follows and watch the Immediate window

    Do While y < 1000
         Debug.Print y
    etc.
    Need some attention to your logic?

  4. #4
    Registered User
    Join Date
    06-07-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Nested loops problem

    Barryleajo, I put in Debug.Print y and nothing happened. any suggestions.

    and thanks shg I didnt even consider using For statements. but now I want to figure out what I did wrong with the Do statements.

  5. #5
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: Nested loops problem

    Hi Symplystyc
    Have you got your Immediate window in view?
    In the VBE editor, open it from the View menu.
    It will likely dock as a window at the bottom of the code window.
    This is the only place that you will see anything "happen".
    It should (does in my test) report the value of y each time it iterates the loop

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1