我正在进行一系列用户表格,并进行问卷调查。每个用户表单由一对选项按钮/复选框组成。该程序工作正常。 但是,当我再次启动程序时(不退出Excel),无线电按钮和复选框的填充方式与之前用户相同。
我尝试使用每个userform初始化来调用模块:
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
最后一种形式不同,有:
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
以上代码有效,但需要更多代码并且效率非常低。
答案 0 :(得分:5)
必须有一种更简单的方法来做到这一点。有关额外信息:之后 每个表格都填写完毕,我用:
Me.Hide form_x.Show
最后一种形式不同,有:
Unload Me
不要在所有表单上使用Hide
,而是使用Unload
。在实际初始化表单时,Userform_Initialize
方法将仅被调用 - 当您显示以前隐藏的表单时,这不会发生。
所以,当你用这些形式做某事时,比如
form1.show 'initialize called
'stuff that changes the form attributes
form1.hide
form1.show 'initialize NOT called
您需要使用Unload
或将代码移到每个UserForm的UserForm_Activate
方法中(这可能会导致其他问题,具体取决于实际使用这些问题的方式)。
答案 1 :(得分:1)
我假设您的数组:“optA”,“optB”等是控件的实际名称。发生此错误是因为optBtn(cnt)
不引用对象,而只引用数组中的字符串值。
基于此来实现您想要的目标,请替换此代码:
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
答案 2 :(得分:0)
这将重置(取消选择)' Userform2Clear'
上的所有选项按钮Dim opt as Control
For Each opt In Userform2Clear.Controls
If TypeName(opt) = "OptionButton" Then opt = False
Next