Consulting

Results 1 to 20 of 20

Thread: Solved: Automatically start the macro in 15mins

  1. #1
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    357
    Location

    Solved: Automatically start the macro in 15mins

    Hi All,

    I want to start the macro after 15mins after it stops first time automatically.

    As I don't have to go and manually start each time.

    Is this possible..????

    If yes, then please provide me the code for the same.


    Thanks
    Sudhir

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    [VBA]Application.OnTime Now + TimeValue("00:00:15"), "my_Procedure"[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I think he wants to do this continually.

    If so, create a Public variable called say nTime of type Double

    at the end of your my_Procedure macro, add

    [vba]

    nTime = Now + TimeValue("00:00:15")
    Application.OnTime nTime, "my_Procedure"
    [/vba]

    and then in the BeforeClose event procedure, add

    [vba]

    Application.OnTime nTime, "my_Procedure",, False
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    357
    Location
    I am initiating the macro on a button click. Once it is over then it should automatically start after 15mins every time, till the file is closed.

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Do you have a problem with xld's code?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    357
    Location
    I am getting the error "Ambiguous name detected: trial" Trial is the name of the procedure.

    Here I am displaying a msgbox, which once done should be diplayed after every 15mins.

    Please help

    Code:


    Public Sub trial()
    Dim nTime As Double
    MsgBox ("Hello")
    nTime = Now + TimeValue("00:00:30")
    Application.OnTime nTime, "trial"

    End Sub

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Ambiguous name usually means that you have two subs with the same name....

    In the sub you posted in post #6 you have a sub named trial....and you are calling a procedure within it named "trial".....that is probably the problem...
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    so how do you define a procedure?

  9. #9
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    357
    Location
    Thanks Lucas,

    I have made some changes after ur advise, still facing the same error.
    Now I am calling trial from another procedure. Attaching code for ur reference.

    Please let me know how to go about it.

    Sub trial()
    MsgBox ("Hello")
    End Sub
    Sub automate()
    Dim nTime As Double
    trial
    nTime = Now + TimeValue("00:00:30")
    Application.OnTime nTime, "trial"

    End Sub

  10. #10
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Runs ok for me ......I changed it to 10 seconds because I'm impatient

    run automate and you get the messagebox.
    close the messagebox and wait 10 seconds.....messagebox again.

    if you're still having problems i would guess that you have another sub named trial or automate....

    see attached.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Ok I see....it doesn't keep repeating....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  12. #12
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    357
    Location
    U have used "Option Explicit" can you please tell what exactly is it..??

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is your module called trial as well by any chance?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    BTW the code should be like this.

    In a standard code module

    [vba]

    Public nTime As Double

    Sub trial()
    MsgBox ("Hello")
    nTime = Now + TimeValue("00:00:30")
    Application.OnTime nTime, "trial"
    End Sub
    [/vba]

    In ThisWorkbook

    [vba]


    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime nTime, "trial", , False
    End Sub

    Private Sub Workbook_Open()
    trial
    End Sub
    [/vba]

    This is workbook event code.
    To input this code, right click on the Excel icon on the worksheet
    (or next to the File menu if you maximise your workbooks),
    select View Code from the menu, and paste the code
    Last edited by Bob Phillips; 10-31-2007 at 05:04 PM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  15. #15
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You can call it again from the messagbox sub
    [VBA]Option Explicit
    Public nTime As Double
    Sub trial()
    MsgBox ("Hello")
    automate
    End Sub
    Sub automate()
    'trial
    nTime = Now + TimeValue("00:00:05")
    Application.OnTime nTime, "trial"
    End Sub[/VBA]

    Problem is it is not looping Bob...so I just called automate again each time from the sub trial
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  16. #16
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    357
    Location
    Thanks Lucas,
    Can u please tell why and when option explicit is used

  17. #17
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    357
    Location
    Hi Guys Thanks.

    Now I got to know more than one way of doing this.

    Thanks Lucas and Xld

  18. #18
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Quote Originally Posted by kbsudhir
    Thanks Lucas,
    Can u please tell why and when option explicit is used
    From the help files:
    If used, the Option Explicit statement must appear in a module before any procedures.
    When Option Explicit appears in a module, you must explicitly declare all variables using the Dim, Private, Public, ReDim, or Static statements. If you attempt to use an undeclared variable name, an error occurs at compile time.
    If you don't use the Option Explicit statement, all undeclared variables are of Variant type unless the default type is otherwise specified with a Deftype statement.
    Note Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear.

    It helps avoid misnamed variables, etc. by throwing an error.....
    It is a good code practice
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  19. #19
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    When to use Option Explicit......always
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  20. #20
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    357
    Location
    Thanks once again

Posting Permissions

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