Chip said:
The only way I can think of to terminate running code is to modify the loop
(I assume the running code is in some sort of Loop structure) to test the
value of a Public variable, whose value would be set by your command button.
For example, declare a Public variable named Done as a Boolean in the
module containing the loop code and write your command button code as
Thanks for the feedback. Looking at the code below, there is already a
request to clear the data from the 'archive' worksheet, so a dual
function button will not be necessary, just a stop button for the loop.
This is the code below, but I am not sure where to enter your code as
I'm a novice at these things.
.............................................................................
Private Sub CommandButton1_Click()
Application.EnableEvents = False
PauseTime = 30 'Pause (in seconds)
'This bit clears the Archive sheet and formats cells to numbers
with 2dps
Sheets("Archive").Select
Sheets("Archive").Cells.Select
Selection.ClearContents
Selection.NumberFormat = "0.00"
Sheets("Data").Select
Sheets("Data").Cells(20, 6) = 0 'Record number of copies
'Start by pasting the horsenames
'Only do this once so outside the loop
Worksheets("Data").Range("A5:A19").Copy
Worksheets("Archive").Select
Worksheets("Archive").Cells(1, 1).Select 'Paste the results in
the top row in Archive
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=True 'Paste values not
formula and transpose
Sheets("Data").Select 'Switch view back to
Data sheet
'Start loop to transfer prices every 30 secs.
Do
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Sheets("Data").Cells(3, 1) = Int(Timer - Start) 'Display
secs since last copy on sheet
Loop
Worksheets("Data").Range("F5:F19").Copy
reqRow = Sheets("Data").Cells(20, 6) + 2 'Paste the results
in the next row in Archive
Worksheets("Archive").Select
Worksheets("Archive").Cells(reqRow, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=True 'Paste values not
formula
Sheets("Data").Select 'Switch view back to
Data sheet
Sheets("Data").Cells(20, 6) = Sheets("Data").Cells(20, 6) + 1
'Count number of copies
Loop Until Sheets("Data").Cells(20, 6) = 10 'Alter this for number
of copies
Sheets("Data").Cells(3, 1) = "Finished"
Application.EnableEvents = True
End Sub