在两个用户窗体之间切换时,宏第二次挂起显示用户窗体
Macro Hangs Second Time Showing a Userform when toggling between two userforms
我有两个用户表单。第一个表单具有复选框,当检查正确时,它会导致表单消失(隐藏)和第二个表单出现。第二种形式提出一些问题,当它关闭(卸载)时,它将答案写入位于第一种形式代码中的字符串变量。 (以另一种形式写入另一个变量的部分不会影响我的问题,因为我已经将其注释掉并且问题仍然存在。)
作为表格 2 userform_terminate
函数的一部分,它重新显示表格 1。
第一次按预期运行;复选框,表格1消失,表格2出现,关闭表格2,表格1出现。
在第二次尝试时,第二种形式将显示,但它的 userform_initialize
似乎没有触发(没有调试消息和断点没有激活),并且宏停在行 userform2.show
包含在 userform1 的代码中(见下文)。
关闭按钮和右上角的x在按下时会有动画,但没有任何反应。唯一的出路是重置密码。
第二条线索是Userform2中的第二条调试消息,Userform1.show
之后的一条没有打印。
第三条线索是,在用户窗体 1 中 Userform2.show
之后立即设置的调试消息不会出现,直到两个用户窗体都关闭(2 然后 1)。
由于Userform1.show
,Userform2 似乎没有完全终止。
这是代码片段。
调用 Userform2 的 Userform1 Sub 以及似乎停止的位置:
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,里面全是这个:
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
我尝试了显示和隐藏顺序的各种组合,我认为这是 中给出的答案。
作为变通方法,我可以改用 userform2.hide,它似乎有效。但它涉及额外的代码来处理使用右上角的 x 而不是关闭按钮的情况。
所以我想我了解到的是,在显示 userform2 时暂停执行 userform1,在重新显示 userform1 时暂停执行 userform2。因此,当 userform1 尝试重新显示 userform2 时,它无法加载,因为它从未完成卸载。
这是让我工作的代码。
Userform1
Private Sub InputCheckBox_Change()
If InputCheckBox.value = True Then
Userform1.Hide
Userform2.Show 'While Userform2 is being show, the code seems to halt here
Userform1.Show 'Used to be in Userform2_Terminate or _QueryClose
End If
End Sub
Userform2,再次完整
Private Sub DefineRepeatableCloseButton_Click()
Unload DefineRepeatable
End Sub
Private Sub UserForm_Initialize()
Debug.Print "DefineRepeatable initialized"
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Bunch of stuff happens here
End Sub
但是我找到了一些代码示例,这些示例显示将 Userform1.show
放入 Userform2 的终止函数中,或者两个表单同时处于活动状态的示例。所以我敢肯定,关于 VBA 表格还有一些我不理解的地方,所以如果有人有很好的参考资料可以解释其中的一些事情,我将不胜感激。
谢谢
我有两个用户表单。第一个表单具有复选框,当检查正确时,它会导致表单消失(隐藏)和第二个表单出现。第二种形式提出一些问题,当它关闭(卸载)时,它将答案写入位于第一种形式代码中的字符串变量。 (以另一种形式写入另一个变量的部分不会影响我的问题,因为我已经将其注释掉并且问题仍然存在。)
作为表格 2 userform_terminate
函数的一部分,它重新显示表格 1。
第一次按预期运行;复选框,表格1消失,表格2出现,关闭表格2,表格1出现。
在第二次尝试时,第二种形式将显示,但它的 userform_initialize
似乎没有触发(没有调试消息和断点没有激活),并且宏停在行 userform2.show
包含在 userform1 的代码中(见下文)。
关闭按钮和右上角的x在按下时会有动画,但没有任何反应。唯一的出路是重置密码。
第二条线索是Userform2中的第二条调试消息,Userform1.show
之后的一条没有打印。
第三条线索是,在用户窗体 1 中 Userform2.show
之后立即设置的调试消息不会出现,直到两个用户窗体都关闭(2 然后 1)。
由于Userform1.show
,Userform2 似乎没有完全终止。
这是代码片段。
调用 Userform2 的 Userform1 Sub 以及似乎停止的位置:
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,里面全是这个:
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
我尝试了显示和隐藏顺序的各种组合,我认为这是
作为变通方法,我可以改用 userform2.hide,它似乎有效。但它涉及额外的代码来处理使用右上角的 x 而不是关闭按钮的情况。
所以我想我了解到的是,在显示 userform2 时暂停执行 userform1,在重新显示 userform1 时暂停执行 userform2。因此,当 userform1 尝试重新显示 userform2 时,它无法加载,因为它从未完成卸载。
这是让我工作的代码。
Userform1
Private Sub InputCheckBox_Change()
If InputCheckBox.value = True Then
Userform1.Hide
Userform2.Show 'While Userform2 is being show, the code seems to halt here
Userform1.Show 'Used to be in Userform2_Terminate or _QueryClose
End If
End Sub
Userform2,再次完整
Private Sub DefineRepeatableCloseButton_Click()
Unload DefineRepeatable
End Sub
Private Sub UserForm_Initialize()
Debug.Print "DefineRepeatable initialized"
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Bunch of stuff happens here
End Sub
但是我找到了一些代码示例,这些示例显示将 Userform1.show
放入 Userform2 的终止函数中,或者两个表单同时处于活动状态的示例。所以我敢肯定,关于 VBA 表格还有一些我不理解的地方,所以如果有人有很好的参考资料可以解释其中的一些事情,我将不胜感激。
谢谢