Remove Dynamically Added Controls from Userform

前端 未结 6 561
野的像风
野的像风 2021-01-02 01:13

I have an Excel userform with dynamically added checkboxes.

I add the checkboxes with code that looks like this:

Set chkBox = Me.Controls.Add(\"Forms         


        
6条回答
  •  猫巷女王i
    2021-01-02 02:03

    if you already know the name of the controls, the type, and how many, why double loop ?

    note that ONLY controls created at runtime can be removed.

    '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
    

    and for your case : 'if all naming are correct

    Dim j&
    For j = 1 To NumControls
        Me.Controls.Remove "Checkbox" & j
    Next j
    

提交回复
热议问题