+ Reply to Thread
Results 1 to 4 of 4

Deal With Recursive (Endless Loop)

Hybrid View

  1. #1
    Registered User
    Join Date
    04-22-2013
    Location
    Ankara
    MS-Off Ver
    Excel 2007
    Posts
    30

    Question Deal With Recursive (Endless Loop)

    Hi all,

    I have a VBA code to find largest value according to the capacity comparison. But if the result of comparison does not get "true", I have to stop the for loop of "k"
    and make it start at the beginning of for loop of j.

    Sub Compare()
        Dim EstMax As Range
        Dim RealMax As Range
    
        Sheets("Sayfa2").Activate
        Set EstMax = Range("A" & Rows.Count)
        
        For j = 2 To 62
        
            For i = 2 To 62
                If Cells(i, 2) > EstMax.Value Then
                    Set EstMax = Cells(i, 2)
                    Row = i
                End If
                
            Next i
            
            a = 0
            
            If Row < 7 Then
                a = 8 - Row
            End If
            
            k = Row - 1
          
            For k = Row - 1 To k = (Row - 7 + a) Step -1
                If k = 0 Then
                    Exit For
                Else
                    If Cells(k, 2) = 0 Then
                        If Cells(Row, 3) + Cells(k, 3) < 8 Then
                            Set RealMax = EstMax
                        Else
                            EstMax = -1
                        'Must be stopped and should begin at beginning of loop for j
                        End If
                    End If
                End If
            Next k
      
        Next j
    
    
        MsgBox RealMax.Value & " is in cell " & RealMax.Address & " in row " & Row
                
    End Sub
    If I call another macro as a result of false, then I have recursive VBA.

    Sub Compare()
        Dim EstMax As Range
        Dim RealMax As Range
    
        Sheets("Sayfa2").Activate
        Set EstMax = Range("A" & Rows.Count)
        
        For j = 2 To 62
        
            For i = 2 To 62
                If Cells(i, 2) > EstMax.Value Then
                    Set EstMax = Cells(i, 2)
                    Row = i
                End If
                
            Next i
            
            a = 0
            
            If Row < 7 Then
                a = 8 - Row
            End If
            
            k = Row - 1
          
            For k = Row - 1 To k = (Row - 7 + a) Step -1
                If k = 0 Then
                    Exit For
                Else
                    If Cells(k, 2) = 0 Then
                        If Cells(Row, 3) + Cells(k, 3) < 8 Then
                            Set RealMax = EstMax
                        Else
                            EstMax = -1
                            Call Compare2
                        End If
                    End If
                End If
            Next k
      
        Next j
    
    
        MsgBox RealMax.Value & " is in cell " & RealMax.Address & " in row " & Row
                
    End Sub
    
    Sub Compare2()
    
        Dim EstMax As Range
        Dim RealMax As Range
    
        Sheets("Sayfa2").Activate
        Set EstMax = Range("A" & Rows.Count)
        
        For j = 2 To 62
        
            For i = 2 To 62
                If Cells(i, 2) > EstMax.Value Then
                    Set EstMax = Cells(i, 2)
                    Row = i
                End If
                
            Next i
            
            a = 0
            
            If Row < 7 Then
                a = 8 - Row
            End If
            
            k = Row - 1
          
            For k = Row - 1 To k = (Row - 7 + a) Step -1
                If k = 0 Then
                    Exit For
                Else
                    If Cells(k, 2) = 0 Then
                        If Cells(Row, 3) + Cells(k, 3) < 8 Then
                            Set RealMax = EstMax
                        Else
                            EstMax = -1
                            Call Compare2
                        End If
                    End If
                End If
            Next k
      
        Next j
    
    
        MsgBox RealMax.Value & " is in cell " & RealMax.Address & " in row " & Row
                
    End Sub

    What should I do to deal with this problem?

    Thanks in advance,
    Oxco

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Deal With Recursive (Endless Loop)

    Have you tried putting "Exit for" after the line "EstMax = -1"?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    04-22-2013
    Location
    Ankara
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Deal With Recursive (Endless Loop)

    Yes, but it does not work! =(

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Deal With Recursive (Endless Loop)

    There are a couple of things I don't follow.

    The blue code below doesn't change any values for each j loop, so nothing changes in the k loop. So if you were to exit k and reset to j=2, what would be different?. You don't use the value of j anywhere within its loop.

    Also, the red code doesn't seem correct. k = (Row - 7 + a) evaluates to True or False which is a 1 or 0. Is that what you want?

    I can't tell you what to fix because I don't follow what it is suppose to do?

    Sub Compare()
        Dim EstMax As Range
        Dim RealMax As Range
    
        Sheets("Sayfa2").Activate
        Set EstMax = Range("A" & Rows.Count)
        
        For j = 2 To 62
        
            For i = 2 To 62
                If Cells(i, 2) > EstMax.Value Then
                    Set EstMax = Cells(i, 2)
                    Row = i
                End If
                
            Next i
            
            a = 0
            
            If Row < 7 Then
                a = 8 - Row
            End If
            
            k = Row - 1
          
            For k = Row - 1 To k = (Row - 7 + a) Step -1
                If k = 0 Then
                    Exit For
                Else
                    If Cells(k, 2) = 0 Then
                        If Cells(Row, 3) + Cells(k, 3) < 8 Then
                            Set RealMax = EstMax
                        Else
                            EstMax = -1
                        'Must be stopped and should begin at beginning of loop for j
                        End If
                    End If
                End If
            Next k
      
        Next j
    
    
        MsgBox RealMax.Value & " is in cell " & RealMax.Address & " in row " & Row
                
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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