Paused macro stops after closing irrelevant workbook

时间:2019-03-19 14:52:39

标签: excel vba

I have macro, where I need to do something in one Sub, then there are others Subs for pause -> do what you need and wait till button is clicked -> continue. This code:

Public Resume_Macro As Boolean

Public Sub Resume_Click
    Resume_Macro = True
End Sub

Public Sub Pause_Macro
    Call CreateButton
    Resume_Macro = False
    MsgBox "Click the button for continue."
    While Not Resume_Macro
        DoEvents
    Wend
    Resume_Macro = False
    MsgBox "Macro will continue." 'You can put it in there if you want a message
End Sub

Public Sub CreateButton
    Application.ScreenUpdating = False
    ActiveSheet.Buttons.Add(1650, 20, 81, 36).Select
    Selection.Characters.Text = "Continue"
    Selection.Name = "Resume"
    Selection.OnAction = "Resume_Click"
    ActiveSheet.Shapes("Resume").Select
    Range("A1").Select 'Put this in your code else the button will be selected!
    Application.ScreenUpdating = True
End Sub

Everything works fine, but when macro is waiting for Resume click and I open and close some other workbook (.xlsx and which do nothing with main workbook), the macro automatically stops.

Of course I could live with that and do not open any others workbooks while macro is paused, but sometimes I just need to check something and this is obstacle for me.

Thank you for any advice.

0 个答案:

没有答案