Excel auto save macro doesn't execute when cursor is active in a cell

随声附和 提交于 2020-01-24 00:53:27

问题


I have a macro which auto saves the workbook every 5 minutes to avoid losing data. The workbook is set up with a data connection to collect production data from a PLC controller and production operators can also enter notes into the workbook.

The problem is that if someone started entering a note but didn't confirm the entry by pressing enter or tab or by clicking on a different cell then the auto save macro will not execute and the workbook will not be saved until the focus changes to another cell.

I tried to change the active cell in the macro right before executing the save statement, but that didn't work. Is there a way to accept the entered (but not confirmed) cell contents before attempting to save the file? Is there another solution that I haven't thought about?

This is the macro code:

Sub AutoSaveAs()
    dTime = Time + TimeValue("00:05:00")
    With Application
        .OnTime dTime, "AutoSaveAs"
        .EnableEvents = False
        .DisplayAlerts = False
        ActiveCell.Offset(1, 0).Select 'This is the code where I tried to change the active cell
        ThisWorkbook.SaveAs "//ThisFilePath/ThisWorkbookName"
        .EnableEvents = True
    End With
End Sub

回答1:


You cannot run a macro while editing a cell. You need user to click another cell or press enter.

See this one of many search result: https://stackoverflow.com/questions/26848266/start-vba-macro-when-editing-a-cellunles



来源:https://stackoverflow.com/questions/43530314/excel-auto-save-macro-doesnt-execute-when-cursor-is-active-in-a-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!