完成表格后清除单选button

我有一系列的用户表单,进行问卷调查。 每个用户窗体包含一对选项button/checkbox。 程序工作正常。 但是,当我再次启动程序(不退出Excel)时,按照与以前的用户相同的方式填充单选button和checkbox。

我尝试每个用户窗体初始化调用一个模块:

Option base 0 Sub clearBtns() Dim optBtn(10) As Variant optBtn(0) = "optA" optBtn(1) = "optB" optBtn(2) = "optC" optBtn(3) = "optD" optBtn(4) = "optE" optBtn(5) = "chkA" optBtn(6) = "chkB" optBtn(7) = "chkC" optBtn(8) = "chkD" optBtn(9) = "chkE" optBtn(10) = "chkF" Dim cnt As Integer For cnt = 0 To 10 If Not optBtn(cnt) Is Nothing Then optBtn(cnt).Value = False End If Next cnt End Sub 

不幸的是这不起作用(错误424,所需的对象)。 这可能与这部分有关:

 If Not optBtn(cnt) Is Nothing Then 

必须有一个更简单的方法来做到这一点。 额外信息:每个表格完成后,我使用:

 Me.Hide form_x.Show 

最后的forms是不同的,有:

 Unload Me 

也许有问题?

进一步解释:

 Option Base 0 Public Sub clearBtns() Dim optBtn(10) As Variant optBtn(0) = "optA" optBtn(1) = "optB" optBtn(2) = "optC" optBtn(3) = "optD" optBtn(4) = "optE" optBtn(5) = "chkA" optBtn(6) = "chkB" optBtn(7) = "chkC" optBtn(8) = "chkD" optBtn(9) = "chkE" optBtn(10) = "chkF" Dim formArray(27) As Variant formArray(0) = "page1_1" formArray(1) = "page1_2" formArray(2) = "page1_3" formArray(3) = "page2_1" formArray(4) = "page2_2" formArray(5) = "page2_3" formArray(6) = "page3_1" formArray(7) = "page3_2" formArray(8) = "page3_3" formArray(9) = "page4_1" formArray(10) = "page4_2" formArray(11) = "page4_3" formArray(12) = "page5_1" formArray(13) = "page6_1" formArray(14) = "page6_2" formArray(15) = "page6_3" formArray(16) = "page7_1" formArray(17) = "page7_2" formArray(18) = "page7_3" formArray(19) = "page8_1" formArray(20) = "page8_2" formArray(21) = "page8_3" formArray(22) = "page9_1" formArray(23) = "page9_2" formArray(24) = "page9_3" formArray(25) = "page10_1" formArray(26) = "page10_2" formArray(27) = "page10_3" Dim cnt As Integer Dim fCnt As Integer For fCnt = 0 To 27 For cnt = 0 To 10 On Error Resume Next formArray(fCnt).Controls(optBtn(cnt)).Value = False Next cnt Next fCnt End Sub 

以上代码不起作用。

 Public Sub clearBtns() Dim optBtn(10) As Variant optBtn(0) = "optA" optBtn(1) = "optB" optBtn(2) = "optC" optBtn(3) = "optD" optBtn(4) = "optE" optBtn(5) = "chkA" optBtn(6) = "chkB" optBtn(7) = "chkC" optBtn(8) = "chkD" optBtn(9) = "chkE" optBtn(10) = "chkF" Dim cnt As Integer For cnt = 0 To 10 On Error Resume Next page1_1.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page1_2.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page1_3.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page2_1.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page2_2.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page2_3.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page3_1.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page3_2.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page3_3.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page4_1.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page4_2.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page4_3.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page5_1.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page6_1.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page6_2.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page6_3.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page7_1.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page7_2.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page7_3.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page8_1.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page8_2.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page8_3.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page9_1.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page9_2.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page9_3.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page10_1.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page10_2.Controls(optBtn(cnt)).Value = False Next cnt cnt = 0 For cnt = 0 To 10 On Error Resume Next page10_3.Controls(optBtn(cnt)).Value = False Next cnt On Error GoTo 0 End Sub 

上面的代码工作,但它需要更多的代码,是非常低效的。

必须有一个更简单的方法来做到这一点。 额外信息:每个表格完成后,我使用:

 Me.Hide form_x.Show 

最后的forms是不同的,有:

 Unload Me 

在所有表单上使用“ Hide而不是使用“ UnloadUserform_Initialize方法只会在窗体实际初始化时被调用 – 当您显示以前的隐藏窗体时,不会发生这种情况。

所以,当你用这些forms做一些事情

 form1.show 'initialize called 'stuff that changes the form attributes form1.hide form1.show 'initialize NOT called 

您需要使用Unload或将您的代码移动到每个用户窗体的UserForm_Activate方法(这可能会导致您其他问题取决于如何实际使用)。

我假设你的数组:“optA”,“optB”等是控件的实际名称。 发生此错误的原因是optBtn(cnt)不引用对象,而只引用数组中的string值。

基于这一点,以实现你所希望的,replace下面的代码:

 For cnt = 0 To 10 If Not optBtn(cnt) Is Nothing Then optBtn(cnt).Value = False End If Next cnt 

与此代码:

 On Error Resume Next For cnt = 0 To 10 Me.Controls(optBtn(cnt)).Value = False Next cnt On Error GoTo 0 

这将重置(取消select)“Userform2Clear”上的所有选项button

 Dim opt as Control For Each opt In Userform2Clear.Controls If TypeName(opt) = "OptionButton" Then opt = False Next