Stopping A Looping of Files At Half Way

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
209
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.
 

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
2,482
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
 

shknbk2

Board Regular
Joined
Mar 5, 2016
Messages
235
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:

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
209
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?
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,407
Office Version
2016
Platform
Windows
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
 

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
209
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?
 

shknbk2

Board Regular
Joined
Mar 5, 2016
Messages
235
Create a new form (assuming "Form1") with a toggle button (assuming "ToggleButton1").

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

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
209
Create a new form (assuming "Form1") with a toggle button (assuming "ToggleButton1").

In your code:
Code:
[COLOR=#0000ff]Load[/COLOR] Form1
Form1.Show vbModeless
[COLOR=#0000ff]Do While[/COLOR] ...  'Your original Do loop
    DoEvents
[COLOR=#0000ff]    If[/COLOR] Form1.ToggleButton1.Value = [COLOR=#0000ff]True Then Exit Do[/COLOR]
    'The rest of your Do code here
[COLOR=#0000ff]Loop[/COLOR]
Unload Form1
thanks. it worked!
 
Last edited:

Forum statistics

Threads
1,077,618
Messages
5,335,267
Members
399,009
Latest member
twcaddell

Some videos you may like

This Week's Hot Topics

Top