+ Reply to Thread
Results 1 to 9 of 9

Basic Loop command

  1. #1
    Registered User
    Join Date
    10-06-2003
    Location
    Ashford, Kent - Working in London
    MS-Off Ver
    Office 365
    Posts
    95

    Basic Loop command

    Hi Excel World!

    I know this should be dead easy for me by now, but I've looked and searched and can't find code that meets my very simple requirements.
    I want code that will simply run a macro causing the worksheet to recalculate for a period of seconds specified in A1 then stop.

    That's it! It's just for a bit of fun thing that I'm working on in my job.

    If anyone could help I would be very grateful.

    Have a great day!
    Big Chris

  2. #2
    Forum Contributor funkymonkUK's Avatar
    Join Date
    01-07-2005
    Location
    London, England
    Posts
    500
    search for ontime in the help

    Should get the following

    OnTime Method


    Schedules a procedure to be run at a specified time in the future (either at a specific time of day or after a specific amount of time has passed).

    Syntax

    expression.OnTime(EarliestTime, Procedure, LatestTime, Schedule)

    expression Required. An expression that returns an Application object.

    EarliestTime Required Variant. The time when you want this procedure to be run.

    Procedure Required String. The name of the procedure to be run.

    LatestTime Optional Variant. The latest time at which the procedure can be run. For example, if LatestTime is set to EarliestTime + 30 and Microsoft Excel is not in Ready, Copy, Cut, or Find mode at EarliestTime because another procedure is running, Microsoft Excel will wait 30 seconds for the first procedure to complete. If Microsoft Excel is not in Ready mode within 30 seconds, the procedure won’t be run. If this argument is omitted, Microsoft Excel will wait until the procedure can be run.

    Schedule Optional Variant. True to schedule a new OnTime procedure. False to clear a previously set procedure. The default value is True.

    Remarks

    Use Now + TimeValue(time) to schedule something to be run when a specific amount of time (counting from now) has elapsed. Use TimeValue(time) to schedule something to be run a specific time.
    and the example being

    OnTime Method Example

    This example runs my_Procedure 15 seconds from now.

    Application.OnTime Now + TimeValue("00:00:15"), "my_Procedure"
    This example runs my_Procedure at 5 P.M.

    Application.OnTime TimeValue("17:00:00"), "my_Procedure"
    This example cancels the OnTime setting from the previous example.

    Application.OnTime EarliestTime:=TimeValue("17:00:00"), _
    Procedure:="my_Procedure", Schedule:=False






    Hope that Helps you.

  3. #3
    Registered User
    Join Date
    10-06-2003
    Location
    Ashford, Kent - Working in London
    MS-Off Ver
    Office 365
    Posts
    95
    Thanks Funky Monk,

    I think I'm getting closer, but rather than run 15 from now, I want to run FOR 15 seconds (or a time in seconds specified in a particular cell (A1) ideally) then looping to cause the worksheet to recalculate.

    The highly complex task in hand is to run a random number selector which will run on screen in front of an audience for 15 seconds and pick a lucky winner from a list of people. I've got the worksheet itself sorted, but need a macro that will loop for a specified time when I click on a button causing the worksheet to recalculate and the names to flash past on the screen until the specified time is up and it displays the winner.

    Any further assistance you could give would be much appreciated.

    Many thanks,
    Last edited by Big Chris; 11-29-2006 at 10:39 AM.

  4. #4
    Registered User
    Join Date
    10-06-2003
    Location
    Ashford, Kent - Working in London
    MS-Off Ver
    Office 365
    Posts
    95
    BTW, for "highly complex" read "this should be so easy that I'm embarrassed that I can't suss it" !

  5. #5
    Forum Contributor funkymonkUK's Avatar
    Join Date
    01-07-2005
    Location
    London, England
    Posts
    500
    oh like a lotto draw.

    give me a minute

  6. #6
    Forum Contributor funkymonkUK's Avatar
    Join Date
    01-07-2005
    Location
    London, England
    Posts
    500
    very easy in fact

    Sub startdraw()

    For i = 1 To 5
    ' Cells b4 have the ball drawn value which is a random number from 1 - 10
    Worksheets("Sheet1").Range("b4").Value = Int((10 * Rnd) + 1)
    ' wait for a second
    Application.Wait (Now + TimeValue("0:00:01"))

    Next

    MsgBox "WE HAVE A WINNER!!! CONGRATS " & Worksheets("Sheet1").Range("d4").Value, vbInformation, "Winner"

    End Sub


    See my example
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    10-06-2003
    Location
    Ashford, Kent - Working in London
    MS-Off Ver
    Office 365
    Posts
    95
    That's the baby!

    Thanks for your patience and taking the time Funky Monk - much appreciated!

    Hope the sun's shining on London!

    Chris.

  8. #8
    Forum Contributor funkymonkUK's Avatar
    Join Date
    01-07-2005
    Location
    London, England
    Posts
    500
    well since I work for the civil Service the sun never seems to shine but maybe someday I will find a better paid job when global warming is the norm.

    The weather is not that good at the moment. I surpose its the same in Hertz?

    Glad to be of some kind of assistance to you.

  9. #9
    Registered User
    Join Date
    10-06-2003
    Location
    Ashford, Kent - Working in London
    MS-Off Ver
    Office 365
    Posts
    95
    Aha, therein lies the cost of the better paid jobs....working in Birmingham 135 miles from home in Herts.

    Everything comes at a cost eh?

    Thanks for your help today Funky Monk..... doesn't sound very civil service to me!!!

    Cheers,

    Chris

+ 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