+ Reply to Thread
Results 1 to 7 of 7

Thread: vba code creating infinite loop?

  1. #1
    Forum Contributor
    Join Date
    09-19-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2007
    Posts
    136

    vba code creating infinite loop?

    Hey all,

    To me, it should not be creating infinite loop. It loops through outer array, checking if the end characters of a string match whats in the end_string array. Then for that string that was cleaned, it goes through another loop to remove any characters that match the second array. That is, if the index of second array is at all present in the cleaner string, then those characters should be removed from inner string. Then it outputs results in column B. Problem is when I run this macro, it causes excel to freeze and I have to force quit excel:

    Sub StringChecker()
    
    Dim string_arr() As Variant
    Dim k As Integer
    
    Dim c As Range
    Set c = ActiveSheet.[A1]
    
    end_string = Array(" &", _
                " TR", _
                " SR", _
                " DEFEN")
                
    substring = Array(" JR ", _
                " SR ")
    
    Do While c <> "End Loop"
                
       c.Offset(0, 1) = c
       
       For k = 0 To UBound(end_string)
       
          If Right(c, Len(end_string(k))) = end_string(k) Then
            cleaner_string = Mid(c, 1, Len(c) - Len(end_string(k)))
            
            For l = 0 To UBound(substring)
    
              clean_string = Replace(cleaner_string, end_string(l), "")
             
    
            Next l
             c.Offset(0, 1) = clean_string
          End If
           
       Next k
       
       
    
      
       Set c = c.Offset(1, 0)
    Loop
    
    End Sub
    Force quitting excel is not the behavior I want. I was hoping it would convert this:

    john smith JR & jennifer TR

    to:

    john smith & jennifer

    thanks for response
    Last edited by johnmerlino; 07-18-2011 at 04:45 PM.

  2. #2
    Forum Guru 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Vienna, VA, USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2,257

    Re: vba code creating infinite loop?

    Do you have the string "End Loop" somewhere in column A? It is probably better to look for a blank cell (like the code I posted to solve this problem for you in another thread), rather than an arbitrary string. You are guaranteed to hit a blank cell eventually.
    Making the world a better place one fret at a time
    ||||||

    If someone helped you, please click on the star icon at the bottom of their post

    If your problem is solved, please update the first post:
    EDIT, Go Advanced button, set Prefix to SOLVED

    [code]
    ' Enclose code in tags like this
    [/code]

    Don't attach a screenshot
    --just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.

  3. #3
    Forum Contributor
    Join Date
    09-19-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: vba code creating infinite loop?

    Quote Originally Posted by 6StringJazzer View Post
    Do you have the string "End Loop" somewhere in column A? It is probably better to look for a blank cell (like the code I posted to solve this problem for you in another thread), rather than an arbitrary string. You are guaranteed to hit a blank cell eventually.
    Problem with what you provided was if there was an empty cell in cell a, then the loop ends. But even if there is empty cell in cell a, the next one might not be empty, so I wouldnt want the loop to just end. but actually I did forget to put the string "End Loop" and now the loop does end, but the inner loop does not remove the string " JR " or " SR " if its in cell, even though the REPLACE function should be doing that.

    Thanks for response
    Last edited by johnmerlino; 07-18-2011 at 05:20 PM.

  4. #4
    Forum Guru 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Vienna, VA, USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2,257

    Re: vba code creating infinite loop?

    I don't understand how you are using the variable substring. You are using its array size for your inner For loop, but you never reference the array anywhere in your code.

    I suspect that
    ' this line
    clean_string = Replace(cleaner_string, end_string(l), "")
    
    ' should be changed to this
    clean_string = Replace(cleaner_string, substring(l), "")
    Making the world a better place one fret at a time
    ||||||

    If someone helped you, please click on the star icon at the bottom of their post

    If your problem is solved, please update the first post:
    EDIT, Go Advanced button, set Prefix to SOLVED

    [code]
    ' Enclose code in tags like this
    [/code]

    Don't attach a screenshot
    --just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.

  5. #5
    Forum Contributor
    Join Date
    09-19-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: vba code creating infinite loop?

    Thanks for response.

    I apply this:

    Sub StringChecker()
    
    
    Dim string_arr() As Variant
    Dim k As Integer
    
    
    Dim c As Range
    Set c = ActiveSheet.[A1]
    
    
    end_string = Array(" &", _
                " TR", _
                " SR", _
                " DEFEN")
                
    substring = Array(" SR ", _
                " JR ")
    
    
    Do While c <> "End Loop"
                
       c.Offset(0, 1) = c
       
       For k = 0 To UBound(end_string)
       
          If Right(c, Len(end_string(k))) = end_string(k) Then
              cleaner_string = Mid(c, 1, Len(c) - Len(end_string(k)))
              
            For l = 0 To UBound(substring)
    
              clean_string = Replace(cleaner_string, substring(l), " ")
             
    
            Next l
            
            c.Offset(0, 1) = clean_string
            
          End If
           
       Next k
    
       Set c = c.Offset(1, 0)
    
    
    Loop
    
    
    End Sub
    And for the substring array, it only ever applies the last index. It's as if the indexes before the last get overwritten or something. For example, if JR is last index, then it will only remove JR from string. If SR is last index, then it will only remove SR from string.

    Thanks for response.

  6. #6
    Forum Guru martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    10,788

    Re: vba code creating infinite loop?

    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and a dabbler in Cisco
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  7. #7
    Forum Contributor
    Join Date
    09-19-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: vba code creating infinite loop?

    Quote Originally Posted by martindwilson View Post
    yeah but I asked here 2 days ago. Still cannot figure out issue.

+ 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.2.0