我的问题是在我的用户表单上动态删除文本框。在userform上有一个旋转按钮,用户可以自行决定创建文本框。当我向上旋转旋转按钮时,它将创建最多我设置的文本框。但是,当我向后旋转按钮时,它只会删除最近创建的文本框,并且不会再删除。
创建框的代码如下
Private Sub AgendaFromBox_Change()
Dim BoxValue As Integer
BoxValue = AgendaFromBox.Value
If BoxValue > 10 Then
AgendaFromBox.Value = 10
Exit Sub
End If
If BoxValue = 0 Then
Exit Sub
End If
Dim NewBox As Control
Dim NewLabel As Control
For i = 1 To BoxValue
Set NewBox = Me.Controls.Add("Forms.Textbox.1")
Set NewLabel = Me.Controls.Add("Forms.Label.1")
With NewBox
.Name = "AgendaBox" & i
.Top = 100 + 30 * i
.Left = 20
.Width = 100
.Height = 20
.ControlSource = "'Hidden'!C" & 2 + i
End With
With NewLabel
.Name = "AgendaLabel" & i
.Top = 100 + 30 * i
.Left = 5
.Width = 14
.Height = 20
.Caption = i & "."
End With
Worksheets("Hidden").Range("B" & 2 + i) = i
Next i
NumOutBefore = BoxValue
End Sub
此代码是链接到旋转按钮的文本框的更改事件的一部分。删除框的代码如下:
Private Sub AgendaFromSpinner_Change()
AgendaFromBox.Value = AgendaFromSpinner.Value
Dim BoxValue1 As Integer
Static NumOutBefore As Integer
BoxValue1 = AgendaFromBox.Value
If BoxValue1 > 9 Then Exit Sub
If BoxValue1 < 1 Then Exit Sub
If BoxValue1 < NumOutBefore Then
Controls.Remove "AgendaBox" & i
Controls.Remove "AgendaLabel" & i
End If
NumOutBefore = AgendaFromSpinner.Value
End Sub
此代码是旋转按钮更改事件的一部分。任何想法或想法都会有所帮助。提前谢谢。
答案 0 :(得分:3)
我认为这是您的代码中发生的事情。如果在每个模块的第一行设置断点,然后在单击微调器向上/向下按钮后逐步执行代码,您应该能够验证这一点:
AgendaFromBox_Change()
被触发并构建AgendaBox1
AgendaFromBox_Change()
被触发并构建AgendaBox1
&amp; AgendaBox2
Agendabox1
的 2 副本。 AgendaFromBox_Change()
被触发并构建AgendaBox1
,AgendaBox2
&amp; AgendaBox3
Agendabox2
1}}和 3 AgendaBox1
AgendaFromBox_Change()
被触发并构建另一个 AgendaBox1
&amp; AgendaBox2
AgendaBox1
的 4 副本,其中两个具有随机分配的名称, 3 副本AgendaBox2
AgendaFromSpinner_change()
继续执行,删除AgendaBox3
AgendaFromBox_Change()
被触发并构建另一个 AgendaBox1
AgendaBox1
AgendaFromSpinner_change()
继续执行,删除AgendaBox2
,但至少还有一个其他AgendaBox2something
仍然可见,因此看起来它没有删除它。要解决这个问题,这应该有效:
Private Sub AgendaFromBox_Change()
Static BoxValue As Integer
if BoxValue > AgendaFromBox.Value then
'we need to update BoxValue no matter what
BoxValue = AgendaFromBox.Value
'we're decrementing the spinner - we don't need to do anything else here
Exit sub
else
'we need to update BoxValue no matter what
BoxValue = AgendaFromBox.Value
end if
If BoxValue > 10 Then
AgendaFromBox.Value = 10
Exit Sub
End If
If BoxValue = 0 Then
Exit Sub
End If
Dim NewBox As Control
Dim NewLabel As Control
Set NewBox = Me.Controls.Add("Forms.Textbox.1")
Set NewLabel = Me.Controls.Add("Forms.Label.1")
With NewBox
.Name = "AgendaBox" & boxvalue
.Top = 100 + 30 * boxvalue
.Left = 20
.Width = 100
.Height = 20
.ControlSource = "'Hidden'!C" & 2 + boxvalue
End With
With NewLabel
.Name = "AgendaLabel" & boxvalue
.Top = 100 + 30 * boxvalue
.Left = 5
.Width = 14
.Height = 20
.Caption = boxvalue & "."
End With
Worksheets("Hidden").Range("B" & 2 + i) = boxvalue
'not sure where this came from or what it does.
'I don't see it declared anywhere
NumOutBefore = BoxValue
End Sub
我的猜测是,您的模块中没有声明Option Explicit
,或者在模块顶部公开声明了NumOutBefore
。确保您已声明Option Explicit
- 稍后会省事