+ Reply to Thread
Results 1 to 8 of 8

Macro loop not terminating

Hybrid View

  1. #1
    Registered User
    Join Date
    07-26-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    8

    Macro loop not terminating

    Hi i have a macro that is supposed to execute everytime something is changed on a sheet other than the summary one. the code is as follows

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Dim R As Long
        Dim C As Integer
        Dim num As Integer
        Dim lastrow As Integer
        Dim lastcol As Integer
            
        lastrow = Cells(Rows.Count, "A").End(xlUp).Row
        lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
     
        num = 0
        
        If ActiveSheet.Name = "Summary" Then
            'Do Nothing
        Else
            For R = 2 To lastrow
                If IsEmpty(Cells(R, 1)) Then
                    Cells(R, 2) = ""
                Else
                    num = 0
                    For C = 3 To lastcol
                        If IsEmpty(Cells(R, C)) Then
                            'Do Nothing
                        Else
                            num = num + 1
                        End If
                        Debug.Print ("Congratulationgs you've made it to checkpoint 1")
                    Next C
                    Cells(R, 2) = num
                End If
                Debug.Print ("Congratulationgs you've made it to checkpoint 2")
            Next R
        End If
    End Sub
    The problem is that the code will run and do what its supposed to. But then it won't stop and i have to break it to to unfreeze my excel program. any help?
    Last edited by sachinahj; 07-27-2012 at 01:34 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: why is this not stopping!!

    you are changing worksheet during worksheet change event!

    my untested revision:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Dim R As Long
        Dim C As Integer
        Dim num As Integer
        Dim lastrow As Integer
        Dim lastcol As Integer
       Dim Debug_Mode as boolern
            
      Debug_Mode = true
    
        on error goto ErrorHandler
        Application.enableevents = false
    
        lastrow = Cells(Rows.Count, "A").End(xlUp).Row
        lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
     
        num = 0
        
        If ActiveSheet.Name = "Summary" Then
            'Do Nothing
        Else
            For R = 2 To lastrow
                If IsEmpty(Cells(R, 1)) Then
                    Cells(R, 2) = ""
                Else
                    num = 0
                    For C = 3 To lastcol
                        If IsEmpty(Cells(R, C)) Then
                            'Do Nothing
                        Else
                            num = num + 1
                        End If
                        Debug.Print ("Congratulationgs you've made it to checkpoint 1")
                    Next C
                    Cells(R, 2) = num
                End If
                Debug.Print ("Congratulationgs you've made it to checkpoint 2")
            Next R
        End If
    
       ErrorExit:
       Application.enableevents = true
       Exit Sub
    
      ErrorHandler:
      MsgBox "Error " & Err.Number & " (" & Err.Description & ") " 
      if degug_Mode then
       stop
       resume
     end if
      Goto ErrorExit
      
    End Sub
    Note: I added enableevents = false at the beginning and True at the end. Also addded ErrorHandler to ensure enableevents is always set back to true.
    Last edited by vandan_tanna; 07-27-2012 at 01:05 PM.
    Regards,
    Vandan

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: why is this not stopping!!

    @ vandan_tanna
    You will need
    Application.EnableEvents = True in your ErrorHandler
    ErrorHandler:
      Application.EnableEvents = True 
      MsgBox "Error " & Err.Number & " (" & Err.Description & ") "
      Err.Clear
    This isn't required
    ErrorExit:
       Application.enableevents = true
       Exit Sub
    Last edited by Marcol; 07-27-2012 at 01:21 PM.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: why is this not stopping!!

    Marcol
    if you don't have the errorexit part you'll get an error message every time
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  5. #5
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Macro loop not terminating

    @Marcol:

    I like single point exit. This is why in ErrorHandler I just show the error message and then go to ErrorExit where I reset the EnableEvents and exit the sub. For this particular example, only one item (EnableEvents) has to be reset, but in some cases a lot more needs to be reset. With my method, there is just one place - ErrorExit - where you reset everything, and you get to ErrorExit regardless of error! I guess is a matter of preference.

    You solution will work also as long as you add exit sub before ErrorHandler (as JosephP pointed out)

  6. #6
    Registered User
    Join Date
    07-26-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    8

    Re: Macro loop not terminating

    Do i have to have an error handle?? I just started VBA this week and Id prefer not to put a debug and error handle into this simple code.

    What if i change the sheetchange to sheetcalculate.. when i do this the macro doesn't execute when i hit cmd + =.

    Any suggestions... Id rather have it on sheet calculate anyway but it wasn't working in the first place so thats why i gave up on that.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Macro loop not terminating

    why would you want to not handle errors? you can simplify to
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Dim R As Long
        Dim C As Integer
        Dim num As Integer
        Dim lastrow As Integer
        Dim lastcol As Integer
            
        lastrow = Cells(Rows.Count, "A").End(xlUp).Row
        lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
     
        num = 0
        
        If ActiveSheet.Name = "Summary" Then
            'Do Nothing
        Else
            For R = 2 To lastrow
                If IsEmpty(Cells(R, 1)) Then
                   on error resume next
                   application.enableevents = false
                    Cells(R, 2) = ""
                    application.enableevents = true
                    on error goto 0
               Else
                    num = 0
                    For C = 3 To lastcol
                        If IsEmpty(Cells(R, C)) Then
                            'Do Nothing
                        Else
                            num = num + 1
                        End If
                        Debug.Print ("Congratulationgs you've made it to checkpoint 1")
                    Next C
                    Cells(R, 2) = num
                End If
                Debug.Print ("Congratulationgs you've made it to checkpoint 2")
            Next R
        End If
    End Sub

  8. #8
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Macro loop not terminating

    Apologies, a bit slap dash there ...
    Another way would be
    ErrorHandler:
      Application.EnableEvents = True
      If Err.Number <> 0 Then
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") "
        Err.Clear
      End If

+ 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