我有一系列用户表单,制作问卷。每个用户窗体由几个选项按钮/复选框组成。该程序运行良好。但是,当我再次启动程序(不退出 Excel)时,单选按钮和复选框的填充方式与前一个用户相同。
我尝试使用每个用户窗体初始化来调用模块:
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
上面的代码可以工作,但是它需要更多的代码并且效率很低。