+ Reply to Thread
Results 1 to 12 of 12

Loop a function

  1. #1
    Registered User
    Join Date
    02-17-2009
    Location
    Sydney
    MS-Off Ver
    Excel 2003
    Posts
    21

    Loop a function

    Hi again

    Is it possible to loop this code so that it does it every day? Currently it runs the commands at 7:00, 7:02, 17:00 and 17:02 then stops. The only way I can get it going again is if I close the workbook and re open it.


    Private Sub Workbook_Open()

    Application.OnTime TimeValue("07:00:00"), "CountMail"
    Application.OnTime TimeValue("07:02:00"), "SaveAsTodaysDate"
    Application.OnTime TimeValue("17:00:00"), "CountMail"
    Application.OnTime TimeValue("17:02:00"), "SaveAsTodaysDate"

    End Sub

    Thanks

  2. #2
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Loop a function

    Hi

    Currently, do you open the file manually before it runs at 7:00, 7:02, 17:00 and 17:02, or does it run even if the file is closed?

    I think one solution could be setting it up as a scheduled task of windows.

    Thanks
    Ajay

  3. #3
    Registered User
    Join Date
    02-17-2009
    Location
    Sydney
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Loop a function

    I have to have the Excel File open for it to work.

    It would be great if it could run closed, however I am not too fussed for the minute. I just want it to loop so that it runs at those times everyday.

    What is the best and easiest way to do this?

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Loop a function

    Haven't tried this, but I can't see any reason it wouldn't work.
    Please Login or Register  to view this content.
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  5. #5
    Registered User
    Join Date
    02-17-2009
    Location
    Sydney
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Loop a function

    Unfortunately this will not work, I tried it, and it does not recognise the function. I am guessing this is because I am trying to run the same function within itself.

    How can I just run a loop?

  6. #6
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Loop a function

    Of course, Workbook_Open() is a private Sub
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    02-17-2009
    Location
    Sydney
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Loop a function

    I am getting the error message:

    The macro "C:\<FileName>.xls'!Run_Subs' cannot be found.

    Also in Line 2 of your text, I presume you have a typo Rub=Run

  8. #8
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Loop a function

    You probably have to put Run_Subs() in a standard module. Don't keep it in ThisWorkbook module.
    Correct, just a typo.

  9. #9
    Registered User
    Join Date
    02-17-2009
    Location
    Sydney
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Loop a function

    Thanks for your help - however it was getting too messy.

    I have used the Task Scheduling option so that it opens the excel file 1 minute before. (6:59AM).

    I have also created a new module called "Quit". Which closes the Excel File.

    Sub Quit()

    Application.Quit

    End Sub



    So this is what my formula now looks like:

    Private Sub Workbook_Open()

    Application.OnTime TimeValue("07:00:00"), "CountMail"
    Application.OnTime TimeValue("07:02:00"), "SaveAsTodaysDate"
    Application.OnTime TimeValue("17:02:30"), "Quit"
    Application.OnTime TimeValue("17:00:00"), "CountMail"
    Application.OnTime TimeValue("17:02:00"), "SaveAsTodaysDate"
    Application.OnTime TimeValue("17:02:30"), "Quit"


    End Sub

  10. #10
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Loop a function

    You may want to open the excel file 2-3 minutes before, to be sure it is open before your first procedure fires. (Just a precaution for system running slow on a given day)

  11. #11
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Loop a function

    fcomino;

    Boy, if you think that is getting messy, you are living a charmed life . I wish I had just one file that was only 10 times as messy as what you have. But if it works for you, that's what's important.

  12. #12
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Loop a function

    fcomino;

    I'll keep this thread in my subscriptions list for at least a month, in case you write more. After that you'll have to send me a private message to let me know you're back

+ 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