Application ontime running randomly !!

fxfaraz

New Member
Joined
Jun 11, 2019
Messages
1
Hello ,

I have a vba code which captures the data every 20 second from a dynamically changing cell value and stores in the sheet along with some calcuations using Application.Ontime

In the sheet , on checking the difference with the timestamp, show data has been entered at 20 seconds most of the time but on 3 secs , 27 sec , 21 secs , etc randomly

Please help.

My code to call the subroutine :

Code:
Sub Workbook_Open()




Sheets("Sheet1").Cells.Clear
Sheets("Sheet2").Cells.Clear
MsgBox ("Hi,  Be ready ")


Application.OnTime TimeValue("09:00:00"), "Calculator"






End Sub

This is the Subroutine

Code:
Sub Calculator()
Dim i, val1, val2, val3, val4, Checker, Checker1 As Double
Application.OnTime Now + TimeValue("00:00:20"), "Calculator"




i = Worksheets("Sheet2").Range("J1")


'Paste timestamps and Close price


Worksheets("Sheet2").Range("A" & i) = Worksheets("Sheet1").Range("B5")
Worksheets("Sheet2").Range("B" & i) = Worksheets("Sheet1").Range("F2")


'Paste buyers and sellers


Worksheets("Sheet2").Range("F" & i) = Worksheets("Sheet1").Range("N2")
Worksheets("Sheet2").Range("G" & i) = Worksheets("Sheet1").Range("M2")


'Ema calucation
'10 period & 20 period multiplier is different ( 0.1818 for 10 period and 0.0952 for 20 period )




If i = 10 Then
Worksheets("Sheet2").Range("C" & i).Formula = "=AVERAGE(B1:B10)"


ElseIf i > 10 Then
val1 = Worksheets("Sheet2").Range("B" & i)
val2 = Worksheets("Sheet2").Range("C" & i - 1)


Worksheets("Sheet2").Range("C" & i) = val1 * 0.1818 + val2 * (1 - 0.1818)




    If i = 20 Then
    Worksheets("Sheet2").Range("D" & i).Formula = "=AVERAGE(B1:B20)"




    ElseIf i > 20 Then
    val3 = Worksheets("Sheet2").Range("B" & i)
    val4 = Worksheets("Sheet2").Range("C" & i - 1)


    Worksheets("Sheet2").Range("D" & i) = val3 * 0.0952 + val4 * (1 - 0.0952)
    
        If (val1 * 0.1818 + val2 * (1 - 0.1818)) > (val3 * 0.0952 + val4 * (1 - 0.0952)) Then
        Worksheets("Sheet2").Range("E" & i) = "B"
        
        Else
        Worksheets("Sheet2").Range("E" & i) = "S"
        
        End If
        
        
    End If




End If










' Check buyer and sellers and compare


If (Worksheets("Sheet1").Range("N2")) > (Worksheets("Sheet1").Range("M2")) Then
Worksheets("Sheet2").Range("H" & i) = "B"


Else
Worksheets("Sheet2").Range("H" & i) = "S"


End If


'increment counter
Worksheets("Sheet2").Range("J1") = i + 1








End Sub

and here is the sheet showing the different times it has run

https://drive.google.com/open?id=1UJhYZ7ev4mOcFPdxE3c3WAxiBCD_IOph

open
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Other clk processes are likely to get in the way unless you have a dedicated machine that does nothing else
 
Upvote 0
Try putting the instruction at the end.

Code:
Sub Calculator()
    Dim i, val1, val2, val3, val4, Checker, Checker1 As Double
    
    i = Worksheets("Sheet2").Range("J1")
    'Paste timestamps and Close price
    Worksheets("Sheet2").Range("A" & i) = Worksheets("Sheet1").Range("B5")
    Worksheets("Sheet2").Range("B" & i) = Worksheets("Sheet1").Range("F2")
    'Paste buyers and sellers
    Worksheets("Sheet2").Range("F" & i) = Worksheets("Sheet1").Range("N2")
    Worksheets("Sheet2").Range("G" & i) = Worksheets("Sheet1").Range("M2")
    'Ema calucation
    '10 period & 20 period multiplier is different ( 0.1818 for 10 period and 0.0952 for 20 period )
    If i = 10 Then
        Worksheets("Sheet2").Range("C" & i).Formula = "=AVERAGE(B1:B10)"
    ElseIf i > 10 Then
        val1 = Worksheets("Sheet2").Range("B" & i)
        val2 = Worksheets("Sheet2").Range("C" & i - 1)
        Worksheets("Sheet2").Range("C" & i) = val1 * 0.1818 + val2 * (1 - 0.1818)
        If i = 20 Then
            Worksheets("Sheet2").Range("D" & i).Formula = "=AVERAGE(B1:B20)"
        ElseIf i > 20 Then
            val3 = Worksheets("Sheet2").Range("B" & i)
            val4 = Worksheets("Sheet2").Range("C" & i - 1)
            Worksheets("Sheet2").Range("D" & i) = val3 * 0.0952 + val4 * (1 - 0.0952)
            If (val1 * 0.1818 + val2 * (1 - 0.1818)) > (val3 * 0.0952 + val4 * (1 - 0.0952)) Then
                Worksheets("Sheet2").Range("E" & i) = "B"
            Else
                Worksheets("Sheet2").Range("E" & i) = "S"
            End If
        End If
    End If
    ' Check buyer and sellers and compare
    If (Worksheets("Sheet1").Range("N2")) > (Worksheets("Sheet1").Range("M2")) Then
        Worksheets("Sheet2").Range("H" & i) = "B"
    Else
        Worksheets("Sheet2").Range("H" & i) = "S"
    End If
    'increment counter
    Worksheets("Sheet2").Range("J1") = i + 1
    
[COLOR=#0000ff]    DoEvents[/COLOR]
[COLOR=#0000ff]    Application.OnTime Now + TimeValue("00:00:20"), "Calculator"[/COLOR]
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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