Stopping A Looping of Files At Half Way
Results 1 to 9 of 9

Thread: Stopping A Looping of Files At Half Way
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Nov 2018
    Posts
    200
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Stopping A Looping of Files At Half Way

    I am reading a bunch of text files in a folder through loop.
    Using possibly a command button, how can i halt the loop half way instead of waiting for it to complete before i can do some other things.

  2. #2
    Board Regular lrobbo314's Avatar
    Join Date
    Jul 2008
    Location
    California
    Posts
    2,381
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Stopping A Looping of Files At Half Way

    Don't think it can be done via a command button. You can always throw in a 'DoEvents' line inside your for loop, then you can hit Ctrl + Break to halt the code. Or, you can figure out what the midway point of the loop would be and add a if statement with a debug print inside that you could put a breakpoint on in the debugger.

    Code:
    Sub eg()
    For i = 1 To 100
        If i = 50 Then
            Debug.Print
        End If
    Next i
    End Sub
    To add code to a workbook. Hit Alt+F11. Hit Alt+I+M to insert new module. Then paste code.
    Array formulas must be entered by hitting Ctrl+Shift+Enter.

    We can't solve problems by using the same kind of thinking we used when we created them.

    Imagination is more important than knowledge.

  3. #3
    Board Regular
    Join Date
    Mar 2016
    Posts
    157
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Stopping A Looping of Files At Half Way

    I do it this way:

    1) Create a form with a ToggleButton
    2) Prior to the loop, load and display the form Modeless
    3) Once each loop iteration, perform DoEvents and check the Value of the ToggleButton
    4) If True, exit loop
    5) After loop, unload form

    Optionally, the form can have a Label updating the status of the loop.
    Last edited by shknbk2; Jul 31st, 2019 at 06:03 PM.

  4. #4
    Board Regular
    Join Date
    Nov 2018
    Posts
    200
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Stopping A Looping of Files At Half Way

    Quote Originally Posted by shknbk2 View Post
    I do it this way:

    1) Create a form with a ToggleButton
    2) Prior to the loop, load and display the form Modeless
    3) Once each loop iteration, perform DoEvents and check the Value of the ToggleButton
    4) If True, exit loop
    5) After loop, unload form

    Optionally, the form can have a Label updating the status of the loop.
    can you please write this in a code for me to get you properly?

  5. #5
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,305
    Post Thanks / Like
    Mentioned
    38 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Stopping A Looping of Files At Half Way

    Something like this maybe:
    Code:
    Option Explicit
    
    Dim bAbort As Boolean
    
    Sub RunLoop()
        Dim i As Long
        
        bAbort = False
        For i = 1 To 100000000
            If bAbort Then bAbort = False: Exit For
            Debug.Print i
            DoEvents
        Next i
        MsgBox "Loop Aborted."
    End Sub
    
    
    Sub AbortLoop()
        bAbort = True
    End Sub
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

  6. #6
    Board Regular
    Join Date
    Nov 2018
    Posts
    200
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Stopping A Looping of Files At Half Way

    Quote Originally Posted by Jaafar Tribak View Post
    Something like this maybe:
    Code:
    Option Explicit
    
    Dim bAbort As Boolean
    
    Sub RunLoop()
        Dim i As Long
        
        bAbort = False
        For i = 1 To 100000000
            If bAbort Then bAbort = False: Exit For
            Debug.Print i
            DoEvents
        Next i
        MsgBox "Loop Aborted."
    End Sub
    
    
    Sub AbortLoop()
        bAbort = True
    End Sub
    will this work for do while .... loop?
    also, is the AbortLoop sub meant for the togglebutton?

  7. #7
    Board Regular
    Join Date
    Mar 2016
    Posts
    157
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Stopping A Looping of Files At Half Way

    Create a new form (assuming "Form1") with a toggle button (assuming "ToggleButton1").

    In your code:
    Code:
    Load Form1
    Form1.Show vbModeless
    Do While ...  'Your original Do loop
        DoEvents
        If Form1.ToggleButton1.Value = True Then Exit Do
        'The rest of your Do code here
    Loop
    Unload Form1

  8. #8
    Board Regular
    Join Date
    Nov 2018
    Posts
    200
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Stopping A Looping of Files At Half Way

    Quote Originally Posted by shknbk2 View Post
    Create a new form (assuming "Form1") with a toggle button (assuming "ToggleButton1").

    In your code:
    Code:
    Load Form1
    Form1.Show vbModeless
    Do While ...  'Your original Do loop
        DoEvents
        If Form1.ToggleButton1.Value = True Then Exit Do
        'The rest of your Do code here
    Loop
    Unload Form1
    thanks. it worked!
    Last edited by yinkajewole; Aug 1st, 2019 at 11:55 AM.

  9. #9
    Board Regular
    Join Date
    Mar 2016
    Posts
    157
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Stopping A Looping of Files At Half Way

    You're welcome. Glad to help.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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