+ Reply to Thread
Results 1 to 13 of 13

OnTime Method error

  1. #1
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179

    OnTime Method error

    I need a macro to run at a certain time on Wednesday's, and then a certain time on the other days.

    I would like to use a cell to reference that time, but right now, I am just hard coding it, but I an not get it to run.

    Ultimately what I am looking for is something like this:

    Please Login or Register  to view this content.
    But I was not getting that to work, so then I just did a standard time code to test it, but even then it didn't work.

    Standard code:

    Please Login or Register  to view this content.
    With the module code:

    Please Login or Register  to view this content.
    But it didn't work....why? What did I do wrong?
    Last edited by dsrt16; 12-24-2009 at 09:03 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: OnTime Method error

    Hello dsrt16,

    The syntax shown below is correct, provided the cell contains a Time value. Have you tried calling the macro from another procedure other than the Workbook_Open event?
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

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

    Re: OnTime Method error

    If the cell contains a time only, then the sub is scheduled to run today or tomorrow at the specified time, whichever has not yet occurred.

    If you want to schedule it later, you need to include the date.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179

    Re: OnTime Method error

    shg: I wanted it to occur every day at that time, and I tested it at 5:10 pm, and it didn't run.

    Leith: I am glad to know my syntax is correct, but then again it didn't even run when I coded the time into the macro. I have not called it from anything else. I thought I needed the open event to get it running.

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

    Re: OnTime Method error

    Try this as a confidence-building exercise, and then modify the code incrementally from there. Demo will run 10 seconds after you run Test.
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179

    Re: OnTime Method error

    Well ultimately I need the code to run at the time listed in the cell. Not incrementally from 12. Example: If the cell says, 2:45, then the code needs to run at 2:45.

  7. #7
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179

    Re: OnTime Method error

    Ok I got it to work hand coding the time when I ran it from a different macro.

    I used this coding to call it:

    Please Login or Register  to view this content.
    OpenMaster macro:
    Please Login or Register  to view this content.
    This all worked fine, but when I changed it to reference a cell I had errors.

    Please Login or Register  to view this content.
    I put the if weekday in comments becasue when I tried to run that, it said argument was not optional.

    After putting that in comments, I tried to run the macro, and the debugger highlighted the last line.

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

    Re: OnTime Method error

    Please Login or Register  to view this content.
    What is "Weekday"? It's not declared in your code. If you're referring to the VBA Weekday function, it needs an argument (a date).

    Please Login or Register  to view this content.
    If E11 contains a time (or date and time), then the syntax is
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179

    Re: OnTime Method error

    Shg: Thank you so much for correcting my syntax. It works perfect.

    Now I just need to find a way to have it run at a different time Wednesday.

    I knew the weekday= 3 was wrong.

    I need a way to tell the code to run at the time posted in cell E11 if it is a Wednesday and cell E12 if it is a different day of the week.

    Any ideas?

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

    Re: OnTime Method error

    I need a way to tell the code to run at the time posted in cell E11 if it is a Wednesday
    If what is a Wednesday? E11? Today?

  11. #11
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179

    Re: OnTime Method error

    If the day the program is run is a Wednesday.

    This program will be used Monday-Friday, and it needs to open the master at a different time on wednesday.

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

    Re: OnTime Method error

    Perhaps like either of these:
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    11-15-2008
    Location
    Arizona
    MS-Off Ver
    Excel 2013
    Posts
    179

    Re: OnTime Method error

    I used the second one, and while it ran with no errors for the time listed in E12 (the non-Wednesday time), I had troubles getting it run at the time listed in E11 when I changed the code to Thursday, so I could test it.

    Instead I decided to do the following

    1) On the sheet I typed =Today() in cell D2
    2) On the sheet I typed =Weekday(D2) in cell D3
    3) In cell E3 I created this formula =IF(D3=4, E11, E12)

    Then I had the macro run at the time listed in cell E3.

    So cell E3 will change to a different time on Wednesday (E11) and another time on the other days (E12)

    I tested this, and it works.

    Thanks for your help. I would not have been able to arrive at this conclusion without your help since you fixed my syntactical error.

+ Reply to 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