删除用户表单中动态创建的文本框的问题

时间:2015-04-17 17:27:20

标签: excel vba excel-vba userform

我的问题是在我的用户表单上动态删除文本框。在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

此代码是旋转按钮更改事件的一部分。任何想法或想法都会有所帮助。提前谢谢。

1 个答案:

答案 0 :(得分:3)

我认为这是您的代码中发生的事情。如果在每个模块的第一行设置断点,然后在单击微调器向上/向下按钮后逐步执行代码,您应该能够验证这一点:

  1. Spinner从0
  2. 开始
  3. 单击微调器 UP 按钮。
    1. Spinner值递增
    2. 框值增加。
    3. AgendaFromBox_Change()被触发并构建AgendaBox1
  4. 单击
  5. Spinner UP 按钮
    1. Spinner值递增
    2. 框值增加。
    3. AgendaFromBox_Change()被触发并构建AgendaBox1&amp; AgendaBox2
      • 您现在拥有Agendabox1 2 副本。
      • 由于VBA不喜欢这样,第二个会自动重命名为
  6. 单击
  7. Spinner UP 按钮
    1. Spinner值递增
    2. 框值增加。
    3. AgendaFromBox_Change()被触发并构建AgendaBox1AgendaBox2&amp; AgendaBox3
      • 您现在拥有Agendabox2 3 AgendaBox1
      • 副本的 2 副本
      • VBA会自动将重复项重命名为
  8. 单击
  9. Spinner DOWN 按钮
    1. Spinner值递减
    2. 箱值减少。
    3. AgendaFromBox_Change()被触发并构建另一个 AgendaBox1&amp; AgendaBox2
      • 您现在拥有AgendaBox1 4 副本,其中两个具有随机分配的名称, 3 副本AgendaBox2
      • 名称可能不是随机的,但它们不是您所期望的。
    4. AgendaFromSpinner_change()继续执行,删除AgendaBox3
  10. 单击
  11. Spinner DOWN 按钮
    1. Spinner值递减
    2. 箱值减少。
    3. AgendaFromBox_Change()被触发并构建另一个 AgendaBox1
      • 您现在拥有AgendaBox1
      • 5 副本
    4. AgendaFromSpinner_change()继续执行,删除AgendaBox2,但至少还有一个其他AgendaBox2something仍然可见,因此看起来它没有删除它。
  12. 要解决这个问题,这应该有效:

    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 - 稍后会省事