Stopping A Looping of Files At Half Way

yinkajewole

Active Member
Joined
Nov 23, 2018
Messages
281
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.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
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
 
Upvote 0
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:
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top