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

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,138
Office Version
  1. 365
Platform
  1. Windows
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

Active Member
Joined
Mar 5, 2016
Messages
259
Office Version
  1. 365
Platform
  1. Windows
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

Active Member
Joined
Nov 23, 2018
Messages
281
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
8,203
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

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

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

Active Member
Joined
Mar 5, 2016
Messages
259
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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

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

shknbk2

Active Member
Joined
Mar 5, 2016
Messages
259
Office Version
  1. 365
Platform
  1. Windows
You're welcome. Glad to help.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,523
Messages
5,636,820
Members
416,943
Latest member
kitkat22

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
Top