Search code examples
vbaexcelpause

Pause Excel macro while Windows is locked


I have a macro which runs an SQL query, which triggers a long slow recalculation of the spreadsheet based on the new data. When it finishes I've used application.speech to alert me if certain conditions have been met; all this works fine. But:

As I said, the recalculation can take quite a while, so I sometimes lock Windows and go do something else while I wait. Does anyone know of a way to pause the macro so that it doesn't run the last few lines (i.e. the audio alerts) until Windows is unlocked?


Solution

  • Before you call Application.speech you could check if the system is locked and if it is locked you repeat the check every minute until it is unlocked. Code if a system is locked

    Sub SystemLocked()
    
        Do While Check_If_Locked = "Locked"
            Application.Wait TimeValue("00:00:01")
        Loop
    
        MsgBox "Done"
    
    End Sub
    

    Code for Check_If_Locked can be found here https://www.mrexcel.com/forum/microsoft-access/646623-check-if-system-locked-unlocked-using-vba.html