need code to loop a macro, updating a chart and allowing a user controlled exit from the loop

jey_sea

New Member
Joined
Apr 23, 2010
Messages
24
Hi, I'm having trouble writing a continuious loop that works: Here's the issue.

I have a command button that calls a macro (Continuious) which has the following code:



Sub Continuious ()

DIM ContinuiousIsRunning as Boolean
If ContinuiousIsRunning then
ContinuousIsRunning = False
End
End IF
ContinuiousIsRunning=True
Do
DoEvents
Call Module1.MakeMeasurement ' Note this routine will make a
measurement, fill the result in an
array, the Array is linked to a chart
that shows the result of the
measurment.
DoEvents
Loop
End Sub

Here's the problems:
1)the chart never updates as the array changes with new data
2) I was hoping that the command button would still be active during the loop in order to hit another time to stop the endless loop.

Any help would be appreciated.

Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Here's one way you could toggle the MakeMeasurement macro.

Initially set your CommandButton caption to be Start

Code:
Private Sub CommandButton1_Click()
    
    If CommandButton1.Caption = "Start" Then
        ' Start measurments
        CommandButton1.Caption = "Stop"
        Application.OnTime Now, "MakeMeasurement"
    Else
        ' Stop measurments
        CommandButton1.Caption = "Start"
    End If
    
End Sub

Code:
Private Sub MakeMeasurement()
    
    If Sheets("Sheet1").CommandButton1.Caption = "Start" Then Exit Sub
    
    'Make mesurments code goes here
    
    ' Loop every second
    Application.OnTime Now + TimeValue("00:00:01"), "MakeMeasurement"
    
End Sub

As far as why your chart doesn't update, that may have to do with the MakeMeasurement code.
 
Upvote 0
Thanks AlphaFrog; I will try your suggestion for controlling the loop; as far as the chart updating; the data collected fro the instrument is dropped into a range of cells 1 column by 201 rows; the chart's series has the correct range of cells listed; Question; after the execution of the makemeasurment routine and during the wait statement, do I need to execute a calculation command or something in order to get the chart to refresh?

Thanks again; I'll get back to you with the result of the control issue. JC
 
Upvote 0
Alphfrog; your assist to break out of the loop worked for me; much appreciated. Also I might add that the charts seem to be updating now.

Again, many thanks....
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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