Why won't Application.Ontime work for me?.....

timsp

New Member
Joined
Jun 12, 2008
Messages
8
I have a spreadsheet with a macro that dynamically records new lines of data every time a stock price changes. It works fine, except that it starts as soon as I open the workbook. I always open the workbook about an hour before the stock market opens, and I would like the macro to start at 9:30 EST.

I tried using Application.Ontime, but my workbook still starts writing new lines of data as soon as I open it. Any help is appreciated.

This is my code:

Private Sub Workbook_Open()
'Runs a macro at 3:30 PM


Application.OnTime TimeValue("15:30:00"), "Worksheet_Calculate"


End Sub








Private Sub Worksheet_Calculate()



nextrow = Range("G65536").End(xlUp).Row + 1
If Cells(nextrow - 1, 7) <> Range("d4") _
And Abs(Cells(nextrow - 1, 7) - Range("d4")) >= 0.02 _
And Int(Range("d4") / 0.02) = Range("d4") / 0.02 Then
Application.EnableEvents = False
Cells(nextrow, 7) = Range("d4")
Cells(nextrow, 8) = Application.WorksheetFunction.Round(Range("d5"), 3)
' Cells(nextrow, 9) = Right(Now, 11)
Cells(nextrow, 9) = Now()
Cells(nextrow, 10) = Range("d3")
Cells(8, 4) = Range("d4")
Cells(9, 4) = Application.WorksheetFunction.Round(Range("d5"), 3)
Application.EnableEvents = True
End If

' checks for ratio change V and if true adds line
nextrow = Range("G65536").End(xlUp).Row + 1
If Abs((Cells(nextrow - 1, 8)) - Range("d5")) >= 0.001 And _
(Cells(nextrow - 1, 7) - Range("d4")) = 0 Then
Application.EnableEvents = False
Cells(nextrow, 7) = Range("d4")
Cells(nextrow, 8) = Application.WorksheetFunction.Round(Range("d5"), 3)
' Cells(nextrow, 9) = Right(Now, 11)
Cells(nextrow, 9) = Now()
Cells(nextrow, 10) = Range("d3")
Cells(8, 4) = Range("d4")
Cells(9, 4) = Application.WorksheetFunction.Round(Range("d5"), 3)
Application.EnableEvents = True
End If

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The Worksheet_Calculate is adding the new rows, therefore try this with your existing code placed where indicated. There should be no need for Workbook_Open nor Application.OnTime.
Code:
Private Sub Worksheet_Calculate()
    If Now >= TimeValue("09:30:00") Then
        'PUT YOUR CODE HERE
    End If
End Sub
PS please use CODE tags.
 
Upvote 0
The Worksheet_Calculate is adding the new rows, therefore try this with your existing code placed where indicated. There should be no need for Workbook_Open nor Application.OnTime.
Code:
Private Sub Worksheet_Calculate()
    If Now >= TimeValue("09:30:00") Then
        'PUT YOUR CODE HERE
    End If
End Sub
PS please use CODE tags.

Thank you very much, John. I will do as suggested and run it tomorrow morning.
 
Upvote 0
Rich (BB code):
    If Now >= TimeValue("09:30:00") Then
        'PUT YOUR CODE HERE
    End If

Now() is effectively Date+Time. So that condition will always be true. Instead, try:
Rich (BB code):
If Time >= TimeValue("09:30:00") Then
    '.... your code ....
End if
PS: timsp specifically wrote "9:30 EST" [sic; I suspect he means ET, not EST] and offered an example that starts at "3:30 PM" local time. So perhaps that should be TimeValue("15:30:00").
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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