Vba 用户窗体在使用按钮后冻结

Vba 用户窗体在使用按钮后冻结,vba,excel,userform,Vba,Excel,Userform,我在UserFrom中有一个CommandButton,它执行以下操作: Private Sub CommandButton_Click() If (ModuleExists("myModule") = False) Then MsgBox stringAlert Exit Sub 'This line does not freezes it. End If inputsToValidate = True 'This is

我在UserFrom中有一个CommandButton,它执行以下操作:

 Private Sub CommandButton_Click()

     If (ModuleExists("myModule") = False) Then
         MsgBox stringAlert
         Exit Sub 'This line does not freezes it.
     End If

     inputsToValidate = True 'This is a public variable
     validarInput 'This method validates the userform inputs, if all OK inputsToValidate = False

     If inputsToValidate Then
         Exit Sub 'This line freezes the userform. The code actually continues, but when it does exits the sub it freezes.
     End If

     DoThings

     Unload InterpolateCurveForm

End Sub
正如您在我的注释中所看到的,除了当代码进入“If-inputsToValidate-Then”语句时,代码是有效的。如果发生这种情况,代码实际上会执行Exit子行,但在此之后它会冻结


validarInputs是一个私有子组件,如果任何输入错误,它将显示消息。如果一切正常,则不会显示任何消息,它会将公共变量inputsToValidate更改为False

这是您可以执行的另一种方法:

....

    If inputsToValidate = False Then
        DoThings
        Unload InterpolateCurveForm
    End If

End Sub

我尝试将公共变量更改为局部变量,然后将validarInputs更改为私有函数,因此inputsToValidate的定义如下:
inputsToValidate=validarInputs


这项功能现在运行正常。

请考虑逐一注释一些内容(如注释出
validarInput
和stet
inputsToValidate=False
,以确定它何时不再冻结。因此,您可以确定问题出在代码的哪一部分。这肯定是让函数返回值的更好方法,而不是使用全局变量。还可以将
inputsToValidate
作为变量e> ByRef参数将是一个可能的解决方案。