[問題] 關於VBA 自動存檔的問題

作者: skymt333 (..............)   2022-03-30 14:46:18
我在A檔案寫入
Private Sub workbook_open()
Call AAAsave
End Sub
Private Sub AAASsave()
Application.OnTime Now + TimeValue("00:00:10"), "ThisWorkBook.AAA"
End Sub
Private Sub AAA()
ThisWorkbook.save
Call AAAsave
End Sub
我在B檔寫入
Private Sub workbook_open()
Call BBBsave
End Sub
Private Sub BBBsave()
Application.OnTime Now + TimeValue("00:00:15"), "ThisWorkBook.BBB"
End Sub
Private Sub BBB()
ThisWorkbook.save
Call BBBsave
End Sub
但現在發生一個問題,兩個檔案一起打開後,我關掉B檔案後,可是當A檔案自動存檔時,
B檔案就會被開啟。我關掉A檔後,當B檔案自動儲存開始時,關掉的A檔就會被開啟。
可否有人能指點一下小弟原因,或是這樣寫錯在哪,
萬分感謝
作者: waiter337 (給開司一罐蘇格登)   2022-03-30 20:48:00
Private Sub AAASsave()Set wb1 = NothingOn Error Resume NextSet wb1 = Workbooks("a1.xlsm")On Error GoTo 0If Not wb1 Is Nothing ThenApplication.OnTime Now + TimeValue("00:00:10"), "ThisWorkBook.AAA"End IfEnd SubPrivate Sub BBBsave() 也仿照改 檔名要改還是不行的話 就改下面這個Private Sub AAA()Set wb1 = NothingOn Error Resume NextSet wb1 = Workbooks("a1.xlsm") '這個檔案的檔名On Error GoTo 0If Not wb1 Is Nothing ThenThisWorkbook.SaveCall AAAsaveEnd IfEnd Sub另外補充....你的這個寫法屬於連續觸發的寫法aaasave() 跟aaa() 原本就是一套 被你分開寫了原本應該是這樣寫的Private Sub AAAsave()ThisWorkbook.saveApplication.OnTime Now + TimeValue("00:00:10"),"ThisWorkBook.AAAsave" ,FalseEnd Sub所以最後可以改成Private Sub AAAsave()Set wb1 = Nothing: On Error Resume NextSet wb1 = Workbooks("a1.xlsm"): On Error GoTo 0If Not wb1 Is Nothing ThenThisWorkbook.SaveApplication.OnTime Now + TimeValue("00:00:10"), _"ThisWorkBook.AAAsave", FalseEnd IfEnd Sub總之就是加入了一個判斷工作簿是否開啟才能觸發定時器
作者: skymt333 (..............)   2022-03-30 23:44:00
照W大您的方式修改後好像還是停不住但還是感謝您指出問題點,我再研究一下如何改 感謝
作者: waiter337 (給開司一罐蘇格登)   2022-03-31 10:03:00
作者: skymt333 (..............)   2022-03-31 13:57:00
還讓您弄教學影片,真是不好意思,我晚上下班後研究,感謝啊

Links booklink

Contact Us: admin [ a t ] ucptt.com