Consulting

Results 1 to 10 of 10

Thread: Solved: VBA timer trouble

  1. #1
    VBAX Regular
    Joined
    Feb 2010
    Posts
    33
    Location

    Solved: VBA timer trouble

    Hi Everyone:

    I have a spreadsheet(attached to this mail).I want a macro that starts at 9:45 am runs every 15 minutes that captures range (A1:E10) and copies them to a new worksheet.
    The timer must end at 4:30 pm every day.

    I have written some code,but it does not work,if someone could help me "tweaking" it i would be very grateful

    Regards

    Attachment 2879

  2. #2
    VBAX Regular
    Joined
    Feb 2010
    Posts
    33
    Location
    Oh Sorry here is the code:

    [VBA]
    Public Sub StartTimer()
    ' Workbooks(cWorkbook).Sheets("Summary").Range("B2") = DateDiff("s", TimeLastExecution, Now) / 60
    Workbooks(cWorkbook).Sheets("Summary").Range("B2") = TimeLastExecution
    If TimeLastExecution < Now + TimeSerial(0, 15, 0) Then
    Call Update_SQLFundSummary
    End If

    End Sub


    Public Sub EODStartTimer()

    Application.OnTime TimeValue("09:45:00"), procedure:="StartTimer" 'EOD timer"

    MsgBox "Timer Started"

    End Sub


    [/VBA]




    I basically want a loop that starts the timer at 9:45 and ends it at 4:30 everyday

    Thanks

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You have workbook open and close code in a standard module, it should be in ThisWorkbook. And where does StartTime get initiated?
    ____________________________________________
    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 Regular
    Joined
    Feb 2010
    Posts
    33
    Location
    Hi XLD,

    actually I already corrected it,i put the code in this workbook.

    the start time gets initialized in the EODStartTimer() sub.

  5. #5
    VBAX Regular
    Joined
    Feb 2010
    Posts
    33
    Location
    ok made some changes :


    in This Workbook:

    [vba]Private Sub Workbook_Open()
    Call StartTimer
    End Sub
    [/vba]


    In the module UpdateSummary :


    [vba]

    Public Sub StartTimer()
    ' Workbooks(cWorkbook).Sheets("Summary").Range("B2") = TimeLastExecution
    If TimeLastExecution < Now + TimeSerial(0, 1, 0) Then

    Call Update_SQLFundSummary

    End If

    End Sub

    Public Sub EODStartTimer()

    Application.OnTime TimeValue("09:45:00"), procedure:="StartTimer"

    End Sub

    [/vba]


    now all I need is the timer to start at 9:45 and end at 4:30

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How does the timer get restarted? You should check the tim in StartTimer and not re-start it if after yhour cutoff time.
    ____________________________________________
    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

  7. #7
    VBAX Regular
    Joined
    Feb 2010
    Posts
    33
    Location
    Hi XLD:

    Thanks for the reply,I think I am taking you every which way,Sorry about that: my manager was not very clear in his statement(!)

    here is what I am trying :

    I have 5 traders that use this spreadsheet: i want the subroutine StartTimer to fire off every 15 minutes whenever they open the spreadsheet.It should start at 9:45 am and end at 4:30 pm everyday(except weekends and holidays).The subroutine fires off ONLY if the workbook is open or it is stays open.

    We can ignore the weekend and holidays part for now,its too complicated.

    Any pointers how I can proceed?

    Regards

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, I don't think so, I think you have been clear (can't say the same for your code, which is why I am trying to tell you how to do it rather than do it ).

    What you need is to fire thye Startimer routine in Workbook_Open, as you are now doing.

    StartTimer should check the day and time to see whether it does anything. If within bounds, fire your doing macro. If not, just exit gracefully.

    Something like this

    [vba]

    Public Sub StartTimer()
    If Weekday(Date, 2) < 6 Then

    If Time >= TimeSerial(9, 45, 0) And _
    Time <= TimeSerial (16, 15, 0) Then

    Application.Ontime "StartTimer", Now + TimeSerial(0, 15, 0)

    Call Update_SQLFundSummary
    End If
    End If
    End Sub
    [/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

  9. #9
    VBAX Regular
    Joined
    Feb 2010
    Posts
    33
    Location
    Thanks XLD:

    but i am getting an error on this line :

    Application.OnTime "StartTimer", Now + TimeSerial(0, 1, 0)

    i am using it like this:

    [VBA]

    Private Sub Workbook_Open()
    Call StartTimer
    End Sub

    [/VBA]

  10. #10
    VBAX Regular
    Joined
    Feb 2010
    Posts
    33
    Location
    I slightly modified your proc :

    [VBA]
    Public Sub StartTimer()
    If Weekday(Date, 2) < 6 Then

    If Time >= TimeSerial(9, 45, 0) And _
    Time <= TimeSerial(16, 15, 0) Then

    ' Application.OnTime "StartTimer", Now + TimeSerial(0, 1, 0)
    RunWhen = Now + TimeSerial(0, cRunIntervalMinutes, 0)

    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True

    ' Call Update_SQLFundSummary
    End If

    End If

    End Sub

    [/VBA]

Posting Permissions

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