从Userform中删除动态添加的控件

时间:2015-01-06 00:36:26

标签: excel vba excel-vba userform

我有一个excel Userform,带有动态添加的复选框。我在早期添加了复选框,代码如下:

Set chkBox = Me.Controls.Add("Forms.Checkbox.1", "Checkbox" & i)

稍后,我想删除所有这些复选框。我正在尝试这段代码:

    Dim j As Integer
'Remove all dynamically updated checkboxes
For Each cont In Me.Controls
    For j = 1 To NumControls
    If cont.Name = "Checkbox" & j Then
        Me.Controls.Remove ("Checkbox" & j)
    End If
    Next j
Next cont

但是收到以下错误消息:Error MEssage

任何帮助将不胜感激。

6 个答案:

答案 0 :(得分:12)

更好的方法可能是跟踪您创建的控件(例如在集合中),并使用它来删除它们。

这样,您的代码不会绑定到名称格式,也可以应用于其他控件类型。

Private cbxs As Collection

Private Sub UserForm_Initialize()
    Set cbxs = New Collection
End Sub

' Remove all dynamicly added Controls
Private Sub btnRemove_Click()
    Dim i As Long
    Do While cbxs.Count > 0
        Me.Controls.Remove cbxs.Item(1).Name
        cbxs.Remove 1
    Loop
End Sub


' Add some Controls, example for testing purposes
Private Sub btnAdd_Click()
    Dim i As Long
    Dim chkBox As Control
    For i = 1 To 10
        Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "SomeRandomName" & i)
        chkBox.Top = 40 + i * 20
        chkBox.Left = 20
        cbxs.Add chkBox, chkBox.Name  ' <-- populate tracking collection
    Next

    ' Demo that it works for other control types
    For i = 1 To 10
        Set chkBox = Me.Controls.Add("Forms.ListBox.1", "SomeOtherRandomName" & i)
        chkBox.Top = 40 + i * 20
        chkBox.Left = 60
        chkBox.Add chkBox, chkBox.Name
    Next

End Sub

答案 1 :(得分:2)

假设没有其他控件名称以&#34; Checkbox&#34;,

开头
For Each cont In Me.Controls
    If InStr(cont.Name, "Checkbox") = 1 Then
        Me.Controls.Remove cont.Name
    End If
Next cont

答案 2 :(得分:2)

如果您已经知道控件的名称,类型和数量,为什么要双循环?

请注意,只能删除在运行时创建的ON控件。

def test_this():
    assert pytest.helpers.my_custom_assert_helper(blah) 

def test_this_2():
    assert pytest.helpers.assertme(blah)

def test_this_3():
    assert pytest.helpers.asserts.me(blah)

和您的情况:&#39;如果所有命名都正确

'the following removes all controls created at runtime
Dim i As Long
On Error Resume Next
With Me.Controls
    For i = .Count - 1 to 0 step -1
        .Remove i
    Next i
End With
Err.Clear: On Error GoTo 0

答案 3 :(得分:1)

添加对控件的检查似乎解决了这个问题。不完全确定原因,但它确实有效。

   Dim j As Integer
'Remove all dynamically updated checkboxes
For Each cont In Me.Controls
    If TypeName(cont) = "CheckBox" Then
        For j = 1 To NumControls
            If cont.Name = "Checkbox" & j Then
                Me.Controls.Remove cont.Name
                Exit For
            End If
        Next j
    End If
Next cont

答案 4 :(得分:0)

我使用命令按钮重写了原始代码,只添加了“Me.Controls.Count”而不是“NumControls”并将“Cont”定义为控件。它似乎对我有用。如果这对你有用,请告诉我:

- &GT;

On Error Resume Next
Dim Cont As Control
Dim C As Integer
'Remove all dynamically updated checkboxes
For Each Cont In Me.Controls
    For C = 1 To Me.Controls.Count
    If Cont.Name = "CommandButton" & C Then
        Me.Controls.Remove ("CommandButton" & C)
    End If
    Next C
Next Cont

答案 5 :(得分:0)

选择保留哪些控件和删除哪些控件的另一种方法是使用.Tag属性。 这样可以在添加控件时对控件进行一些精细控制,例如,使用.Tag作为位域。

在创建时:

With Me.Controls.add("Forms.Label.1", Visible:=True)
{code}
    .Tag = 1
{more code}

然后当需要整理时:

For Each C In Me.Controls
    If C.Tag = 1 Then Me.Controls.Remove C.Name
Next