问题与删除用户窗体中dynamic创build的文本框

我的问题涉及在我的用户表单上dynamic删除文本框。 在用户窗体上有一个旋转button,用户可以根据自己的判断来创build文本框。 当我向上旋转旋钮时,它会创build文本框,达到我设置的最大量。 但是,当我向后旋转button,它只会删除最近创build的文本框,不会再删除。

创build框的代码如下

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 

此代码是链接到旋转button的文本框的更改事件的一部分。 删除框的代码如下。

 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 

此代码是旋转button更改事件的一部分。 任何想法或想法都会有所帮助。 先谢谢你。

我认为这是你的代码中发生的事情。 如果你在每个模块的第一行设置了一个断点,那么在单击微调器上/下button之后,单步执行代码,你应该可以validation这一点:

  1. 微调从0开始
  2. 点击微调器UPbutton。
    1. 微调器值递增
    2. 框值递增。
    3. AgendaFromBox_Change()被触发并构buildAgendaBox1
  3. 单击微调button
    1. 微调器值递增
    2. 框值递增。
    3. AgendaFromBox_Change()被触发并构buildAgendaBox1AgendaBox2
      • 你现在有2Agendabox1
      • 由于VBA不会那样,第二个被自动重命名为某个东西
  4. 单击微调button
    1. 微调器值递增
    2. 框值递增。
    3. AgendaFromBox_Change()被触发并构buildAgendaBox1AgendaBox2AgendaBox3
      • 你现在有2Agendabox23AgendaBox1
      • VBA会自动将重复项重命名为某些内容
  5. 单击微调器向下button
    1. 微调值减less
    2. 方块值递减。
    3. AgendaFromBox_Change()被触发并构build另一个 AgendaBox1AgendaBox2
      • 你现在有4AgendaBox1副本, AgendaBox1两个是随机分配的名字和3AgendaBox2副本
      • 名称可能不是随机的,但它们不是你所期望的。
    4. AgendaFromSpinner_change()继续执行,删除AgendaBox3
  6. 单击微调器向下button
    1. 微调值减less
    2. 方块值递减。
    3. AgendaFromBox_Change()被触发并构build另一个 AgendaBox1
      • 你现在有5AgendaBox1副本
    4. AgendaFromSpinner_change()继续执行,删除AgendaBox2 ,但已经有至less一个其他的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声明 – 这将在以后节省麻烦