Board logo

標題: [發問] 如何判斷 Application.OnTime 是否在執行?? [打印本頁]

作者: t8899    時間: 2014-10-17 14:52     標題: 如何判斷 Application.OnTime 是否在執行??

本帖最後由 t8899 於 2014-10-17 14:54 編輯

如何判斷 Application.OnTime 是否在執行中??
語法為何?
IF ................THEN ........
--------------------------------------------
Sub timestock()
my = #12:01:00 AM#
The_Time = Time + my
Application.OnTime The_Time, "timestock"

END SUB
作者: 蝕光迴狼    時間: 2014-10-17 16:46

回復 1# t8899

是這個意思嗎?
讓你知道程式有沒有在跑
  1. Sub timestock()

  2. my = #12:01:00 AM#
  3. The_Time = Time + my
  4. Application.OnTime The_Time, "timestock"
  5. Application.OnTime Now + TimeValue("00:00:01"), "timestock"
  6. [A1] = Time

  7. End Sub
複製代碼

作者: t8899    時間: 2014-10-17 18:43

本帖最後由 t8899 於 2014-10-17 18:57 編輯
回復  t8899

是這個意思嗎?
讓你知道程式有沒有在跑
蝕光迴狼 發表於 2014-10-17 16:46

您的方式應該會佔資源.....??
我是要知道timestock 是否正在跑 ?用 if ....then 的語句 .........
不要用類似  if [a1]=1 then true
Sub timestock()
[a1]=1
my = #12:01:00 AM#
The_Time = Time + my
Application.OnTime The_Time, "timestock"
END SUB

sub timestop()
Application.OnTime The_Time, "timestock", Schedule:=False
[a1]=2
end sub

是不是應該用變數給它???變數又要怎麼設??
作者: GBKEE    時間: 2014-10-17 19:24

回復 3# t8899


   
您的方式應該會佔資源.....??
我是要知道timestock 是否正在跑 ?

是這樣嗎?
  1. Sub timestock()
  2.     Application.DisplayStatusBar = True
  3.     Application.StatusBar = "timestock 正在跑 "
  4.     '你的程式碼
  5.     '你的程式碼
  6.     '你的程式碼
  7.     '你的程式碼
  8.     '你的程式碼
  9.     '你的程式碼
  10.     Application.StatusBar = "timestock 跑完了"
  11. End Sub
複製代碼

作者: bobomi    時間: 2014-10-17 19:31

module 丟入一個 Public 變數名
作者: t8899    時間: 2014-10-17 19:33

回復  t8899


   
是這樣嗎?
GBKEE 發表於 2014-10-17 19:24


if ..timestock 正在跑  then............  語法????
作者: bobomi    時間: 2014-10-17 19:47

if ..timestock 正在跑  then............  語法????
t8899 發表於 2014-10-17 19:33


想要攔截就在 module 放一個
Public Sub AAA( 正在跑 )
If  正在跑 Then
Else
End if
End Sub

    Sub timestock()
         Call Sheet1.AAA ( True )
    End Sub

    Sub  timestop()
         Call Sheet1.AAA ( False )
    End Sub
作者: GBKEE    時間: 2014-10-17 19:49

回復 6# t8899


   
if ..timestock 正在跑  then............  語法????

5#bobomi 建議   module 丟入一個 Public 變數名
timestock 正在跑時可執行一個副程式  ->   if 變數名=true  then ....
但你可能會說 "您的方式應該會佔資源.....??" 所以我 @-+/*
作者: t8899    時間: 2014-10-17 20:03

本帖最後由 t8899 於 2014-10-17 20:06 編輯
想要攔截就在 module 放一個
Public Sub AAA( 正在跑 )
If  正在跑 Then
Else
End if
End Sub

...
bobomi 發表於 2014-10-17 19:47

謝謝!
有時執行
Application.OnTime The_Time, "timestock", Schedule:=False
timestock 無法停止退出 ..也不知有無退出成功...又重覆執行timestock
變成兩個timestock在跑...
您的方法似乎無法達到我的要求..........??
一定要知道timestock 有無在執行中,才可達到我的要求
作者: bobomi    時間: 2014-10-17 20:11

Sub timestock()
Call  AAA ( True ) ' 放在  Application.OnTime The_Time, "timestock" 之前

my = #12:01:00 AM#
The_Time = Time + my
Application.OnTime The_Time, "timestock"

END SUB
作者: t8899    時間: 2014-10-17 20:55

本帖最後由 t8899 於 2014-10-17 20:56 編輯
Sub timestock()
Call  AAA ( True ) ' 放在  Application.OnTime The_Time, "timestock" 之前

my = #1 ...
bobomi 發表於 2014-10-17 20:11
[attach]19361[/attach]
applicaiton.ontime  麻煩真的很大
欲停止 ,執行階段錯誤1004
如何強迫停止???關掉excel ??
作者: 蝕光迴狼    時間: 2014-10-17 22:58

回復 11# t8899


1. 是否可以說明這個程式的用意為何?
 如此才能真正幫你達到,你想要追求的效果。

2. 大部份時間有2種呈現方式【時間計時】與【倒數計時】。

3. 建議你可以設立2個按鈕【程式開始啟動】與【程式停止】會比較好。
 因為有關於時間,程式的寫法都是要即時更近的,就會發生你這種”程式停不下來”的窘境。
作者: t8899    時間: 2014-10-18 06:10

本帖最後由 t8899 於 2014-10-18 06:22 編輯
回復  t8899
1. 是否可以說明這個程式的用意為何?
 如此才能真正幫你達到,你想要追求的效果。
..
蝕光迴狼 發表於 2014-10-17 22:58
1. 是否可以說明這個程式的用意為何?
 如此才能真正幫你達到,你想要追求的效果。

1分鐘執行1次zzzzz程序
有時莫名其妙??秒,會執行1次,這種情形出現後,按停止鈕還在執行,只能關掉excel
Sub IE345678()
If Not Ie Is Nothing Then
MsgBoxTest 0, "IE345678 已執行", "提示訊息", vbSystemModal, 0, 2000
Exit Sub
End If
    Set Ie = CreateObject("InternetExplorer.Application")
    Ie.Navigate "http://newmis.twse.com.tw/stock/group.jsp?ex=tse&ind=TIDX#STATISTICS"
    Do While Ie.Busy Or Ie.ReadyState <> 4: DoEvents: Loop
    timestock
End Sub
-------------------------------------------------
Sub timestock()
   my = #12:01:00 AM#
The_Time = Time + my
Application.OnTime The_Time, "timestock"
    '-------------------------------------------------------
zzzzz ' 主要程序
'------------------------------------------------------------------------------------------
        If Not Ie Is Nothing Then
        Application.OnTime The_Time, "timestock", Schedule:=False
        Ie.Quit
        Set Ie = Nothing
                 End If
Exit Sub




3. 建議你可以設立2個按鈕【程式開始啟動】與【程式停止】會比較好。
 因為有關於時間,程式的寫法都是要即時更近的,就會發生你這種”程式停不下來”的窘境。

我本來就是做兩個按扭,但我也有把這兩個程序擺在 Private Sub Worksheet_Calculate()  裡
Private Sub Worksheet_Calculate()
  If TimeValue(Now) = TimeValue("13:24:51") Then
IE345678
End If

If TimeValue(Now) = TimeValue("13:30:30") Then
ASTOP
End If
end sub
---------------------------------------------------
Sub ASTOP()
        If Not Ie Is Nothing Then
        On Error Resume Next
           Ie.Quit
        Application.OnTime The_Time, "timestock", Schedule:=False
           MsgBoxTest 0, "已停止 ", "提示訊息", vbSystemModal, 0, 350
          End If
   
End Sub
作者: GBKEE    時間: 2014-10-18 07:10

本帖最後由 GBKEE 於 2014-10-18 07:18 編輯

回復 13# t8899
試試看
  1. Option Explicit
  2. Dim The_Time As Date '這模組的私用變數
  3. Sub timestock()
  4.     Dim my As Date
  5.     If The_Time > Time Then
  6.        Exit Sub
  7.         '預防 上一次 OnTime尚未執行,再度執行一次OnTime
  8.     End If
  9.     Application.StatusBar = "執行 timestock  中..."
  10.     my = #12:01:00 AM#
  11.     The_Time = Time + my
  12.     Application.OnTime The_Time, "timestock"
  13.     Application.StatusBar = "下次執行 timestock  時間" & The_Time
  14. End Sub
  15. Sub ASTOP()
  16.     If The_Time < Time Then
  17.         MsgBox "沒有 OnTime 程序 ", , "提示訊息"
  18.     Else
  19.         Application.OnTime The_Time, "timestock", Schedule:=False
  20.         MsgBox The_Time & " 的 OnTime 程序 已停止", , "提示訊息"
  21.         Application.StatusBar = False
  22.         The_Time = 0
  23.     End If
  24. End Sub
複製代碼

作者: bobomi    時間: 2014-10-18 08:23

本帖最後由 bobomi 於 2014-10-18 08:26 編輯

Sub IE345678()

If Not Ie Is Nothing Then
   MsgBox "IE345678 已執行"
   Exit Sub
End If


    Set Ie = CreateObject("InternetExplorer.Application")
        Ie.Visible = 1
        Ie.Navigate "http://newmis.twse.com.tw/stock/group.jsp?ex=tse&ind=TIDX#STATISTICS"
        
    Do While Ie.Busy Or Ie.ReadyState <> 4: DoEvents: Loop
    timestock
End Sub

Sub timestock()

'Application.OnTime The_Time, "timestock"
    '-------------------------------------------------------
'zzzzz ' 主要程序
For w = 1 To 100000000
   
Next

'------------------------------------------------------------------------------------------
        If Not Ie Is Nothing Then
           Ie.Quit
           Set Ie = Nothing         
        End If
       my = 3 / 86400
       The_Time = Time + my
       Application.OnTime The_Time, "IE345678"
End Sub
作者: bobomi    時間: 2014-10-18 08:31

要求 99% 準時就這樣

Sub IE345678()

If Not IE Is Nothing Then
   Application.OnTime The_Time + 1 / 86400, "IE345678"
   Exit Sub
End If


    Set IE = CreateObject("InternetExplorer.Application")
        IE.Visible = 1
        IE.Navigate "http://newmis.twse.com.tw/stock/group.jsp?ex=tse&ind=TIDX#STATISTICS"
        
    Do While IE.Busy Or IE.ReadyState <> 4: DoEvents: Loop
    timestock
End Sub

Sub timestock()


        my = 3  / 86400
        The_Time = Time + my
        Application.OnTime The_Time, "IE345678"

    '-------------------------------------------------------
'zzzzz ' 主要程序
For w = 1 To 100000000
   
Next

'------------------------------------------------------------------------------------------
        If Not IE Is Nothing Then
           IE.Quit
           Set IE = Nothing
        End If
End Sub
作者: t8899    時間: 2014-10-18 08:52

Sub IE345678()

If Not Ie Is Nothing Then
   MsgBox "IE345678 已執行"
   Exit Sub
End If
  ...
bobomi 發表於 2014-10-18 08:23

要測試一段時間,希望能解決問題,謝謝指導 .
作者: t8899    時間: 2014-10-18 08:53

要求 99% 準時就這樣

Sub IE345678()

If Not IE Is Nothing Then
   Application.OnTime The_Time + ...
bobomi 發表於 2014-10-18 08:31


要測試一段時間,希望能解決問題,謝謝指導 .
作者: t8899    時間: 2014-10-21 15:17

回復  t8899
試試看
GBKEE 發表於 2014-10-18 07:10


my = #12:01:00 AM#
是不是這問題 ??上午跑沒問題,下午循還2-3次會跳出??
作者: GBKEE    時間: 2014-10-21 15:28

回復 19# t8899
   
my = #12:01:00 AM#
是不是這問題 ??上午跑沒問題,下午循還2-3次會跳出??

鐵定不是,要附檔看看或是完整程式碼
  1. Sub Ex()
  2.     Dim A As Double
  3.     A = #12:01:00 AM#
  4.     MsgBox A = 60 / 86400
  5. End Sub
複製代碼

作者: t8899    時間: 2014-10-21 15:45

回復  t8899
   
鐵定不是,要附檔看看或是完整程式碼
GBKEE 發表於 2014-10-21 15:28

附檔[attach]19373[/attach]
作者: GBKEE    時間: 2014-10-21 16:50

回復 21# t8899
修改一下
  1. Option Explicit
  2. Public The_Time As Date
  3. Public my As Date
  4. Sub timestock()
  5.     If CDec(The_Time) > CDec(Time) Then    '預防 上一次 OnTime尚未執行,再度執行一次OnTime
  6.       '************************
  7.       '當 my <= #12:00:05 AM# 時
  8.       'If The_Time > Time Then 這條件式常會成立
  9.       '是由於 The_Time As Date型態範圍小
  10.       
  11.       'CDec(The_Time) 傳回 Decimal 資料型態
  12.       'Decimal 資料型態
  13.       'Decimal 變數係以 96 位元 ( 12 個位元組 ) 有正負號的整數形式來儲存。10 的指數以小數點右邊的位元數來調整,範圍從 0 到 28。在沒有小數位數的情形下,最大的可能值為 +/-79,228,162,514,264,337,593,543,950,335。而在有 28 個小數位置的情形下,最大值為 +/-7.9228162514264337593543950335,而最小的非零值為 +/-0.0000000000000000000000000001。
  14.       '附註 此時,Decimal 資料型態只能在 Variant中使用
  15.       '也就是,您並不能宣告一變數為 Decimal 的型態。不過您可用 Cdec 函數,建立一個副型態為 Decimal 的 Variant。
  16.       '************************
  17.         Debug.Print CDec(The_Time), CDec(Time)
  18.         Exit Sub        
  19.     End If
  20.     With Sheets("sheet1").Range("a1")
  21.         If .Value = 1 Then
  22.             my = #12:01:00 AM#
  23.         ElseIf .Value = 2 Then
  24.             my = #12:02:00 AM#
  25.         ElseIf .Value = 3 Then
  26.             my = #12:00:30 AM#
  27.         ElseIf .Value = 4 Then
  28.             my = #12:00:20 AM#
  29.         ElseIf .Value = 5 Then
  30.             my = #12:00:05 AM#
  31.         ElseIf .Value = 6 Then
  32.             my = #12:00:10 AM#
  33.         Else
  34.             Exit Sub
  35.         End If
  36.     End With
  37.     The_Time = Time + my
  38.     Debug.Print The_Time
  39.     Application.OnTime The_Time, "timestock"
  40. End Sub
複製代碼

作者: t8899    時間: 2014-10-21 18:08

回復  t8899
修改一下
GBKEE 發表於 2014-10-21 16:50


測試沒問題了,謝謝
30秒用那個較省資源?
#12:00:30 AM#
TimeSerial(0, 0, 30)
DateAdd("s", 30, Time)
作者: t8899    時間: 2014-10-21 18:57

回復  t8899
修改一下
GBKEE 發表於 2014-10-21 16:50

抱歉,測久一點還是有問題
大約5分30秒 就出錯了
附檔[attach]19374[/attach]
作者: GBKEE    時間: 2014-10-22 05:00

回復 24# t8899
測試20分鐘沒出錯
   
  1. Option Explicit
  2. Public The_Time As Date
  3. Public my As Date
  4. Sub timestock()
  5. If CDec(The_Time) > CDec(Time) Then    '預防 上一次 OnTime尚未執行,再度執行一次OnTime
  6.     Range("C65536").End(xlUp).Offset(1) = Time '**** OnTime尚未執行時,再度執行一次OnTime
  7.     Exit Sub
  8. End If
  9.     If Sheets("sheet1").Range("a1").Value = 1 Then my = #12:01:00 AM#
  10.     If Sheets("sheet1").Range("a1").Value = 2 Then my = #12:02:00 AM#
  11.     If Sheets("sheet1").Range("a1").Value = 3 Then my = #12:00:30 AM#
  12.     If Sheets("sheet1").Range("a1").Value = 4 Then my = #12:00:20 AM#
  13.     If Sheets("sheet1").Range("a1").Value = 5 Then my = #12:00:05 AM#
  14.     If Sheets("sheet1").Range("a1").Value = 6 Then my = #12:00:10 AM#
  15.     Range("a65536").End(xlUp).Offset(1) = Time   '**** 執行的OnTime
  16.     The_Time = Time + my
  17.     Application.OnTime The_Time, "timestock"
  18.     Range("b65536").End(xlUp).Offset(1) = The_Time '**** 執行下一次OnTime
  19.     MsgBoxTest 0, "test", "提示訊息", vbSystemModal, 0, 1000
  20. End Sub
複製代碼

[attach]19377[/attach]
作者: t8899    時間: 2014-10-22 05:12

回復  t8899
測試20分鐘沒出錯
GBKEE 發表於 2014-10-22 05:00

下午才會發生這種錯誤
您是剛測試的嗎?現在早上5:10
麻煩您在控制台把時間改為14:00 再試試
試後再調回來
我是這樣試出來的
作者: GBKEE    時間: 2014-10-22 09:50

本帖最後由 GBKEE 於 2014-10-22 16:04 編輯

回復 26# t8899
請在改一下試試看
  1. If TimeValue(The_Time) > Time Then
複製代碼

作者: t8899    時間: 2014-10-22 14:57

回復  t8899
請在改一下試試看
GBKEE 發表於 2014-10-22 09:50


測試20分鐘,沒什問題
1.TimeValue(The_Time) 跟 The_Time 傳回都是+30秒的值有何不一樣?,格式也沒什麼不同?
2.光用 The_Time 為何到下午就會發生錯誤???
作者: GBKEE    時間: 2014-10-22 15:00

回復 28# t8899
要研究一下




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)