Need to execute macro at specific time

jkupfer

New Member
Joined
Dec 23, 2008
Messages
12
Hello all. Using Excel 2003 with WinXP, I'm trying to run macro code automatically whenever time = 9:30:00 (or whatever time I pick). I've been able to get the time to update fine, but unless I click on the worksheet while the time condition is TRUE then my code doesn't run.

I've tried using the Workbook_SheetChange function, but without luck. Unless I activate the sheet the code doesn't run.

I want to be able to setup this to run the code at X time and then I walk away for the day.

Any help on this would be greatly appreciated. Thanks.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
welcome to MrExcel

do a search on this forum for using the WIndows Scheduler. I have seen some questions asked about this sort of thing.
 
Upvote 0
I had tried the OnTime function, but the same problem existed - even though the time updated my macro code wouldn't run unless the worksheet is manually selected. Maybe I did it wrong. I found a good link on this site that shows me something more detailed that the previous instructions I found: http://www.cpearson.com/excel/OnTime.aspx.

I will try the other search term. I had tried to search other terms, but not that one. Thanks both of you for the help.
:)
 
Last edited:
Upvote 0
Can you post the macro code you have tried?

Where do you have the code located?
Worksheet module?
Thisworkbook module?
Standard module?
 
Upvote 0
OK, after reading some more about the two alternatives proposed (ontime and macro scheduler) I think I need to clarify my request and why they won't work.

The time value I'm using is sent to me from a remote server so that it is an exact time, not time from my system. Therefore, I don't want the macro code to trigger a certain amount of time after I've logged on, but exactly at 9:30:01. I'm using this system for trading stocks, and 9:30 is market open. Since I'm trading stocks this has to be very exact, so I can't trust a macro scheduler to do this. Besides, I have to have the sheet open the remainder of the day to do other stuff.

The code further below is what I'm trying to get to run. The time value is in cell L1. In cell L2 I have the following code:
Code:
=IF(L1="9:30:01",TRUE, FALSE)

That turns to True at 9:30:01 and false before and after that time.

The code below checks cell L2 and then runs the code below that. It works just fine when I click on L2 and the result is True, but unless I manually "activate" the cell the result will turn to True but not run the code.

Placed in ThisWorkbook:
Code:
'Declare order object
Dim Order As SterlingLib.STIOrder
'Declare integer variable to manage loop
Dim intLoop As Integer
'Declare integer variable to hold submit order errors
Dim intSubmit As Integer
Option Explicit

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  
    If Target.Address = "$L$2" Then
        If Target.Value = "True" Then
            'Stop any possible runtime errors and halting code
       
            On Error Resume Next
             
                Application.EnableEvents = False
                
  'Initialize intLoop to the second row to account for the title row
    intLoop = 2
    'Set up Loop to cover all positions until there is a blank line
    Do Until Range("A" & intLoop).Value = vbNullString
        'Initialize Order object
        Set Order = New SterlingLib.STIOrder
        'Determine Side for the order based on column C, Sell if position is Long, Buy if position is Short
        If Range("C" & intLoop).Value > 0 Then
            Order.Side = "S"
        End If
        If Range("C" & intLoop).Value < 0 Then
            Order.Side = "B"
        End If
        'Determine Symbol for the order based on column A
        Order.Symbol = Range("A" & intLoop).Value
        'Set Time in Force for the order to Day
        Order.Tif = "D"
        'Determine Account for the order based on column B
        Order.Account = Range("B" & intLoop).Value
        'Determine Quantity of the order, whether long or short, it will equal the absolute value of the position
        Order.Quantity = Abs(Range("C" & intLoop).Value)
        'Determine Destination for the order based on column F, the default is ARCA
        If Range("F" & intLoop).Value = vbNullString Then
            Order.Destination = "ARCA"
        Else
            Order.Destination = Range("F" & intLoop).Value
        End If
        'Determine Price for the order based on column E, If it is a number it will be a limit price, otherwise it will send a market order
        If IsNumeric(Range("E" & intLoop).Value) Then
            Order.PriceType = ptSTILmt
            Order.LmtPrice = Range("E" & intLoop).Value
        Else
            Order.PriceType = ptSTIMkt
        End If
        'Send the order and capture any errors
        intSubmit = Order.SubmitOrder
        'Display any errors.  Error code of 0 means that the order was submitted successfully.  See the Sterling Trader ActiveX API Guide under the Support Documentation drop down box at http://sterlingfinancialsystems.com/support.php.
        If intSubmit <> 0 Then
            MsgBox ("Submit Order Error " & Str(intSubmit) & ".  See the Sterling Trader ActiveX API Guide for more information.")
        End If
        'Destroy Order object
        Set Order = Nothing
        'Increment intLoop
        intLoop = intLoop + 1
    Loop
                
                
                'Turn events back on
                Application.EnableEvents = True
            'Allow run time errors again
            On Error GoTo 0
        End If
    End If
End Sub







Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
 If Target.Address = "$L$2" Then
        If Target.Value = "True" Then
          MsgBox ("Submit Order Error.  See the Sterling Trader ActiveX API Guide for more information.")
        End If
    End If
End Sub

I hope this clarifies things. Thanks in advance for your help.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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