大家說英文標題:

excel ?a href="http://vbtrax.com/track/clicks/1748/c627c2bd9c0729def09cbd2e8d2b891473624ec970e9f0ab416db302630c">線上英語詣訄绦?如何增加停止巨集

發問:

此為每5秒自動執行排序,請問如何再加一段 停止程式碼?謝謝。-------------------------------------------------------------------------Sub 排序01()'' 排序01 Macro' Su 在 2011/9/16 錄製的巨集'' Range("A1:I101").Select Application.CutCopyMode = False Selection.Sort Key1:=Range("F2"),... 顯示更多 此為每5秒自動執行排序,請問如何再加一段 停止程式碼? 謝謝。 ------------------------------------------------------------------------- Sub 排序01() ' ' 排序01 Macro ' Su 在 2011/9/16 錄製的巨集 '' Range("A1:I101").Select Application.CutCopyMode = False Selection.Sort Key1:=Range("F2"), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _ :=xlPinYin, DataOption1:=xlSortNormal Range("K1:S101").Select Selection.Sort Key1:=Range("P2"), Order1:=xlDescending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _ :=xlPinYin, DataOption1:=xlSortNormal Range("A1").Select End SubSub StartTimer() DoEvents Application.OnTime Now + TimeValue("00:00:05"), "StartTimer2", Schedule:=True End Sub Sub StartTimer2() DoEvents Call 排序01 Call StartTimer End Sub Sub StopTimer() On Error Resume Next Application.OnTime Now + TimeValue("00:00:05"), "StartTimer2", Schedule:=False End Sub

最佳解答:

<範例檔>參考看看: http://www.funp.net/388306 2011-09-22 23:35:37 補充: EXCEL VBA. 如何〔停止〕以〔固定時間〕自動遞迴執行的巨集 <.准提部林.> --------------------------------- 範例檔簡介: ■主要需求: 檔案中的〔排序01〕巨集設定為〔每 5 秒〕自動遞迴執行一次, 在執行當中,如何〔停止〕該程式? ■程式碼1:藉〔頂端宣告區〕設一〔變數〕來控制 Dim uMode '頂端宣告區 '------------------------- Sub 排序01() If uMode = "Stop" Then Exit Sub Range("U1") = Format(Time, "hh:mm:ss") Range("A1:I101").Sort Key1:=Range("F2"), _ Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, _ MatchCase:=False, Orientation:=xlTopToBottom Range("K1:S101").Sort Key1:=Range("P2"), _ Order1:=xlDescending, Header:=xlGuess, OrderCustom:=1, _ MatchCase:=False, Orientation:=xlTopToBottom Beep Application.OnTime Now + TimeValue("00:00:05"), "排序01" End Sub '------------------------- Sub 開始執行() uMode = "Start" Range("U1") = Format(Time, "hh:mm:ss") Application.OnTime Now + TimeValue("00:00:05"), "排序01" End Sub '------------------------- Sub 停止執行() uMode = "Stop" End Sub ■程式碼2:利用儲存格〔T1〕控制,當在 T1 輸入任何值時,停止執行 Sub 排序01() If Range("T1") <> "" Then Exit Sub Range("U1") = Format(Time, "hh:mm:ss") Range("A1:I101").Sort Key1:=Range("F2"), _ Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, _ MatchCase:=False, Orientation:=xlTopToBottom Range("K1:S101").Sort Key1:=Range("P2"), _ Order1:=xlDescending, Header:=xlGuess, OrderCustom:=1, _ MatchCase:=False, Orientation:=xlTopToBottom Beep Application.OnTime Now + TimeValue("00:00:05"), "排序01" End Sub '------------------------- Sub 開始執行() Range("T1").ClearContents '清除T1後執行 Range("U1") = Format(Time, "hh:mm:ss") Application.OnTime Now + TimeValue("00:00:05"), "排序01" End Sub ■注意事項: 上列程式碼中的 Range 前面皆未指明〔工作表〕名稱, 在程式執行當中,如果選取了其它工作表, 程式會以當前工作表為執行對象而發生錯誤! 所以,安全做法,須加上工作表名稱, 例如:Sheets("Sheet1").Range("A1:I101") --------------------------------- <範例檔>: http://www.funp.net/388306 ---------------------------------行動補習網落點分析

其他解答:29077F888C89A7E7
arrow
arrow

    王聖裕姚遼龔財腰 發表在 痞客邦 留言(0) 人氣()