Log in

View Full Version : How To Stop An OnTime Scheduled Macro Without Getting An Error



mrprofit
04-25-2014, 09:28 PM
I have posted this on other forum but with no result, hope anyone here can assist me with the code, thank you

Getting Live MT4 forex data (http://www.excelforum.com/excel-programming-vba-macros/1006214-getting-live-mt4-forex-data.html)

any possible way that i can run the vba and still able to do other task on other workbook?

Excel Fox
05-01-2014, 10:55 AM
Yes, it can be done. I've added a button to make it easier for you to stop the macro in between. Code used

In Module1

Option Explicit
Public blnStopUpdate As Boolean
Dim dtmAlertTime As Date


Private Sub EventMacro()


Dim lngLoop As Integer

For lngLoop = 2 To 2500
If Range("Q" & lngLoop) = Range("$L$2") Then
Range("S" & lngLoop).Value = Range("N2").Value
End If
Next lngLoop
DoEvents
If Not blnStopUpdate Then
dtmAlertTime = Now + TimeValue("00:00:05")
Application.OnTime dtmAlertTime, "EventMacro"
End If


End Sub


Sub GetLiveData()


dtmAlertTime = Now + TimeValue("00:00:05") 'Set time (sec) for updating data
Application.OnTime dtmAlertTime, "EventMacro"


End Sub


Private Sub SetTimeNow()


Dim lngLoop As Integer


Application.ScreenUpdating = False
Range("R2").Value = Evaluate("=INT(NOW()/(1/288))*(1/288)+288*(1/288)*(1/288)")
For lngLoop = 1 + 2 To 2500 + 2
Range("R" & lngLoop).Value = Range("R" & lngLoop - 1).Value + Range("U2").Value
Next lngLoop
Columns("S:S").ClearContents
Range("F2").Select
Application.ScreenUpdating = True

End Sub






In sheet1

Private Sub CommandButton1_Click()

Application.Run "'get Live Data_Test.xls'!GetLiveData"


End Sub


Private Sub CommandButton2_Click()


Application.Run "'get Live Data_Test.xls'!SetTimeNow"


End Sub




Private Sub CommandButton3_Click()


blnStopUpdate = True

End Sub

By the way, the attachment is only meant for MT4 (Meta Trader) users.