I have two userforms. The first form has a checkbox and when checked true it causes the form to disappear (hide) and the second form to appear. The second form asks some questions and when it is closed (unload) it writes the answer to a string variable located within the code of the first form. (The part of writing to another variable in another form doesn't factor into my issue since I have commented it out and the issue persists.)
As a part of form 2's userform_terminate
function it reshows form 1.
The first time it works as expected; Check box, form 1 disappears, form 2 appears, close form 2, form 1 appears.
On the second attempt the second form will show, but its userform_initialize
doesn't seem to trigger (no debug message and break point doesn't activate), and the macro stalls at the line userform2.show
contained in the userform1's code (see below).
The close button and the top right x will animate when they are pressed, but nothing happens. Only way out is to reset the code.
Second clue is that the second debug message in Userform2, the one after Userform1.show
does not print.
Third clue is that the debug message set up immediately after Userform2.show
in Userform1 does not appear until both userforms are closed (2 then 1).
It seems like Userform2 is not fully terminating because of Userform1.show
.
Here are the code snippets.
Userform1 Sub that calls Userform2 and where it seems to stall:
Private Sub InputCheckBox_Change()
If InputCheckBox.Value = True Then
Userform1.Hide
Userform2.Show 'This is the line where it is stalled at if I break the execution
debug.print "This message does not appear until after I've closed both"
End If
End Sub
Userform2, this is everything in it:
Private Sub Userform2CloseButton_Click()
Unload Userform2
End Sub
Private Sub UserForm_Initialize()
Debug.Print "Userform2 initialized"
End Sub
Private Sub UserForm_Terminate()
'Some stuff happens here but for testing I
'commented it out and the issue is still there
debug.print "This message will show up"
Userform1.Show
debug.print "But this message will not"
End Sub
I tried various combinations of the order of shows and hides which I think was answer given in this post.
As a workaround I can use userform2.hide instead and it seems to work. But it involves extra code to handle the case of using the top right x instead of the close button.
Me
inside a form to refer to the form itself. You're using the "default instance" of your forms and that's best avoided if you can - see for example books.google.co.uk/… – Tim WilliamsMe
more. – MidnightRover