Closed Thread
Results 1 to 10 of 10

Need to execute VBA code @ 9:30:01 exactly

  1. #1
    Registered User
    Join Date
    12-27-2008
    Location
    Texas
    Posts
    7

    Need to execute VBA code @ 9:30:01 exactly

    Hello all. Using Excel 2003 with WinXP. I'm trying to run macro code automatically whenever time = 9:30:01 (or whatever time I pick) for a stock market trading program, which is why the exact time matters so much. 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. The time actually is sent to me from the stock data provider and it shows up in a cell as a constantly updating value.

    I've tried using the Workbook_SheetChange function and OnTime method, but without luck. In both cases, unless I activate the sheet the code doesn't run. By activate, I mean that I have to click on the sheet when the values that trigger the code to run would be true. If I do that it works just fine, but sitting around and clicking on a worksheet defeats the purpose of automation. Since I'm trading stocks this has to be very exact, so I can't trust a macro scheduler to do this.

    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:
    Please Login or Register  to view this content.
    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:
    Please Login or Register  to view this content.
    Any help on this would be greatly appreciated. Thanks.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    To have a macro un at a specific time you will need to use the Application.OnTime command


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    12-27-2008
    Location
    Texas
    Posts
    7
    That looks great. I'll try it ASAP. The code that was suggested I try for OnTime previously looked nothing like what you have below. Thank you very much!

    Quote Originally Posted by mudraker View Post
    To have a macro un at a specific time you will need to use the Application.OnTime command


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    With the Worksheet change macro it will cause an error if it has an entry that Excel cannot interept as a time

    Also a Worksheet Change macro will not be triggered on L1 (the cell I used in my code) if L1 has a formula in it.


    Note:- There may be times that Excel is busy and will not be able to start to run the macro at the exact second you specified
    Last edited by mudraker; 12-28-2008 at 07:09 AM.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You need a time and date to schedule a macro with OnTime; otherwise, it will run immediately, since you will have scheduled it to run some time on 1/0/1900.

    One reason that Excel might be busy (and the macro not run) if it it gets left in edit mode -- e.g., you put the cursor in the formula bar and leave it there ...
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326
    SHG, I 'm no good at VBA, but wondering if this function is based on the PC's clock. If this one is not synchronized with stock market trading times, maybe the OP 's macro won't run at the right moment?

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Sure; that's the only time reference there is. But the PC's clock can be synchronized via the internet to be very accurate.

    Might try this, which schedules the macro at the next occurrence of the TOD in cell L1 each time L1 is changed:
    Please Login or Register  to view this content.
    The code MUST go in the appropriate Sheet module.

  8. #8
    Registered User
    Join Date
    01-16-2010
    Location
    Edmonton, Canada
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Need to execute VBA code @ 9:30:01 exactly

    Quote Originally Posted by jkupfer View Post
    The time actually is sent to me from the stock data provider and it shows up in a cell as a constantly updating value.
    How is this done? That would solve the problem that I have:

    My company has several different work sites and the computers are not networked. I have created a workbook that works well as a clock in/out device. The only weakness is that it relies on the clock of individual computers which obviously can be changed prior to clocking in and out. If there is a way to direct Excel to use the date and time from a website then it's good to go.

  9. #9
    Registered User
    Join Date
    06-05-2009
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    50

    Need to execute VBA code @ 9:47 Monday to Friday

    Hi Forum Friends
    I would like to start a macro at 9:47 A.M. automatically. Much like the posting in
    http://www.excelforum.com/excel-prog...1-exactly.html
    I tried to use the code as in #7 (2nd reply by shg).
    If I insert the code I get variable not defined error for ThisFile in the code below and for
    letters x, y, z etc which I use as counters frequently. Can solve the latter by defining them.
    Without the On Time code my macros were working fine.
    My Question, is there a simple On Time procedure that would start a macro at 9:47 and does not cause the define error?

    My SaveAs_Master macro saves 3 copies of the workbook in 3 diffrent drives.

    HTML Code: 
    Please Login or Register  to view this content.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Need to execute VBA code @ 9:30:01 exactly

    Please start your own thread.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1