VBA application Ontime

时间:2016-10-20 19:27:17

标签: excel vba excel-vba

I got some problem with application.ontime maybe somebody knows the solution

From the beggining

Private Sub Workbook_Open()
  'some code
  call macro_name
end sub

sub macro_name
  'some code refreshing data on form
  call start_timer
end sub

dim start_time as date 

Sub start_timer()

    start_time = Now() + TimeValue("00:00:05")
    Application.OnTime start_time, "macro_name"


End Sub

Sub end_timer()

    Application.OnTime start_time, "macro_name", , False
    'MsgBox "done"  i put this to be sure that code is executed 

End Sub

Private Sub Workbook_BeforeClose(cancel As Boolean)
  'some code  
  call end_timer
end sub

problem is that the timer doesn't stop. If i have some other workbook open and i close the one with ontime code it starts again at scheduled time

if i put buttons on a form

Private Sub CommandButton1_Click()
start_timer

End Sub

Private Sub CommandButton2_Click()
end_timer
End Sub

everything works ok and ontime is canceled.

I solved it partly by putting the

call end_timer

on the form closing button

Private Sub Image1_Click()

    end_timer
    ThisWorkbook.Close

End Sub

but if workbook will be closed some other way than button ontime won't be canceled

sorry for my english i'll try to explain better if something is unclear

0 个答案:

没有答案