首页 > 解决方案 > 通过命令 VBA.Userform.Add("UF") 加载用户表单时重新初始化

问题描述

我陷入了VBA编码的问题。有两个名为“UsersForm”和“Registration”的用户表单。

在“UsersForm”中有一个名为 CreateProfile 的命令按钮,点击它应该会显示“Registration”表单。

为此目的,一个名为“ShowNextUF”的过程已编写在一个模块中,我在其中提供如下所示的参数:

Private Sub CreateProfile_Click()  ' <----its an activex Command button of the userform "UsersForm"

 Call ShowNextUF("Registration", Me, True)

End Sub

在模块中:

Sub ShowNextUF(ByVal NxtUF As String, ByVal PrevUF As Object, Optional ByVal UnLd As Boolean = False)

 Dim nUF As Object

 Set nUF = VBA.UserForms.Add(NxtUF)
 If UnLd Then Unload PrevUF
 nUF.Show

End Sub

现在的问题是:当我想检查用户窗体“注册”的文本框的值时,它工作正常..

Private Sub tbStdName_Change()     '<---- A TextBox of "Registration" userform

 StudenName = UCase(tbStdName.Value)

End Sub

但它没有(下);实际上在这种情况下,“注册”表单再次初始化。不知道为什么要重新初始化。

Private Sub tbStdName_Change()     

 StudenName = UCase(Registration.tbStdName.Value)  '<---- Reinitilizing...

End Sub

当我在隐藏“注册”表单后调用该新用户表单时,我需要在另一个新用户表单中使用这个:UCase(Registration.tbStdName.Value)。我哪里错了?

标签: vbaexcelexcel-2007userform

解决方案


@Arno 范博文

感谢您提供的信息,它让我知道“.Add”将在 USerform 中添加新集合......刚刚尝试了这个想法,它得到了解决!

摆脱这种情况的方法是将参数作为对象而不是字符串传递......

Private Sub CreateProfile_Click() 
   Call ShowNextUF(Registration, Me, True) 
End Sub 

并在模块中:

Sub ShowNextUF(ByVal NxtUF As Object, ByVal PrevUF As Object, Optional ByVal UnLd As Boolean = False) 
   If UnLd Then Unload PrevUF 
   NxtUF.Show 
End Sub 

再次感谢...干杯!!!!


推荐阅读