Stopping A Looping of Files At Half Way

yinkajewole

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

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
2,758
Office Version
365, 2019, 2016
Platform
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

Board Regular
Joined
Mar 5, 2016
Messages
240
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
267
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,846
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

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

Watch MrExcel Video

Forum statistics

Threads
1,102,026
Messages
5,484,258
Members
407,436
Latest member
Szafranski

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top