+ Reply to Thread
Results 1 to 9 of 9

Running a macro automatically at the same time everyday

  1. #1
    Registered User
    Join Date
    05-30-2012
    Location
    rockford,il
    MS-Off Ver
    Excel 2003,2010
    Posts
    5

    Running a macro automatically at the same time everyday

    I am trying to write a macro that will save the data in one of the worksheets of a workbook everyday at 11:58 pm. Here is what I have done, It only runs one time and never again. Any help would be appreciated.

    Private Sub Workbook_Open()
    Application.OnTime TimeValue("23:58:00"), "Macro1"
    End Sub

    Private Sub workbook_after_save()
    Application.OnTime TimeValue("23:58:00"), "Macro1"
    End Sub

    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 4/5/2012 by kurtc
    '

    Sheets("Results").Select
    Cells.Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveWorkbook.SaveAs Filename:="H:\ME\Databases\F Series Test Stand\Test Results_" & Format(Date, "ddmmmyyyy") & ".xls", FileFormat:= _
    xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
    , CreateBackup:=False
    ActiveWindow.Close
    Rows("2:1000").Select
    Selection.ClearContents
    Range("A2").Select
    ActiveWorkbook.Save
    End Sub

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Running a macro automatically at the same time everyday

    I haven't tested this, but how about adding something like this at the end of your macro:

    Please Login or Register  to view this content.
    If it was me, I would change the time so that it is not so close to before midnight. If for some reason your macro took over 2 minutes to run, it would set the time for the day after and not the next day.

  3. #3
    Registered User
    Join Date
    05-30-2012
    Location
    rockford,il
    MS-Off Ver
    Excel 2003,2010
    Posts
    5

    Re: Running a macro automatically at the same time everyday

    Other then waiting for 2 days to pass is there a way that I can test this easily?

  4. #4
    Registered User
    Join Date
    05-30-2012
    Location
    rockford,il
    MS-Off Ver
    Excel 2003,2010
    Posts
    5

    Unhappy Re: Running a macro automatically at the same time everyday

    As far as I can tell this is not working. When you say at the end of my macro, where exacly do you mean?

  5. #5
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,028

    Re: Running a macro automatically at the same time everyday

    the code provided by Steven it works.
    try to test it now
    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    Regards, John55
    If you have issues with Code I've provided, I appreciate your feedback.
    In the event Code provided resolves your issue, please mark your Thread as SOLVED.
    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

    ...enjoy -funny parrots-

  6. #6
    Registered User
    Join Date
    05-30-2012
    Location
    rockford,il
    MS-Off Ver
    Excel 2003,2010
    Posts
    5

    Re: Running a macro automatically at the same time everyday

    But will it run everyday at the same time?

  7. #7
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Running a macro automatically at the same time everyday

    I've never tested it, but I will make one observation. Your original Application.OnTime only set the time when the macro should run. My suggestion was to set both the day and the time. "Date + 1" is just another way of saying "tomorrow". Thus Date + 1 + TimeSerial(23, 58, 0) is tomorrow at: 23:58. Because 23:58 is so close to 24:00/00:00, I would recommend moving the time back to 23:30 or some such. Let us know if it works.

  8. #8
    Registered User
    Join Date
    05-30-2012
    Location
    rockford,il
    MS-Off Ver
    Excel 2003,2010
    Posts
    5

    Re: Running a macro automatically at the same time everyday

    I did what you said and will be trying it over the weekend. Should have an answer for you on Monday. I placed this in the ThisWorkbook of the project

    Private Sub Workbook_Open()
    Application.OnTime Date + 1 + TimeSerial(23, 45, 0), "SaveResults"
    End Sub

    and then the SaveResults Macro in a module.

    Sub SaveResults()
    '
    ' SaveResults Macro
    ' Macro recorded 4/5/2012 by kurtc
    '

    Sheets("Results").Select
    Cells.Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveWorkbook.SaveAs Filename:="H:\ME\Databases\F Series Test Stand\Test Results_" & Format(Date, "ddmmmyyyy") & ".xls", FileFormat:= _
    xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
    , CreateBackup:=False
    ActiveWindow.Close
    Rows("2:1000").Select
    Selection.ClearContents
    Range("A2").Select
    ActiveWorkbook.Save
    End Sub

  9. #9
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Running a macro automatically at the same time everyday

    FWIW ... I would have used your original idea.
    This is for today.

    Please Login or Register  to view this content.
    Then at the end of I would add:

    Please Login or Register  to view this content.
    This addition would be for tomorrow and the day after.

+ 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