我得到了以下代码:
Private Sub cboA_change()
'Something to determine number of controls there should be, variable gC
'Something to determine number of controls there are, variable nC
'The first time the code runs, the following code runs:
For i = nC to gC
frmA.Frame1.Controls.Add("txtGroup" & i)
Next
'The second time the code runs, the following is executed:
For i = 7 To nC
Me.Frame1.Controls("txtGroup" & i).Remove 'ERROR HERE
Next
For i = nC to gC
frmA.Frame1.Controls.Add("txtGroup" & i)
Next
End Sub
像这样,代码要大得多,我试图清理它,所以如果结构看起来不正确,那也没关系。
我调试了Add
语句,我知道在用户窗体中添加了一个控件,名为txtGroup7
。但是,当我稍后尝试删除此控件时,我得到了Run-time Error 438: Object Doesn't Support This Property or Method
。我尝试将代码更改为:
Me.Frame1.Controls.Remove ("txtGroup" & i)
但这也没用。
有人能给我指正确的方向吗?
编辑:
我知道帮助说明如下:
"此方法删除运行时添加的任何控件。但是,尝试删除在设计时添加的控件将导致错误。"
但是,由于控件是在运行时添加的(动态地,使用VBA代码),这应该不是问题,对吧?
编辑2:
我不明白为什么这有效,但它似乎有效:
q=0
While q < Me.Frame1.Controls.Count
If Me.Frame1.Controls(q).Name = "txtGroup7" Then
Me.Frame1.Controls.Remove q
Else
q = q + 1
End If
Wend
您的代码中一定有其他错误,因为Remove
应该可以工作。尝试:
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
Me.Frame1.Controls.Add "Forms.TextBox.1", "Text1", True
Else
Me.Frame1.Controls.Remove "Text1"
End If
End Sub
在具有ToggleButton和Frame的UserForm上,按下时可以正确添加和删除TextBox。