remove controls from userform

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
529
At times, I 've wanted to remove; rather then hide, some manually, or dynamically created commandbuttons from a userform. Can anyone show two examples of how to accomplish that with code.

I tried the code below against som of my manually created buttons, but cannot seem to make it work.

Code:
Dim ctl As MSForms.Control
For Each ctl In specialty.Controls
    specialty.Controls.remove ctl
    
Next ctl

Any idea's on how to achieve this?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hiding

Code:
Sub UFtest()
Dim ctl As MSForms.Control
For Each ctl In UserForm1.Controls
    If TypeName(ctl) = "CheckBox" Then ctl.Visible = False
Next ctl
UserForm1.Show
End Sub
 
Upvote 0
'm not sure why and what you;re trying to do....
Why dont you just select and remove it?

I dont see .remove.... property [Me.CommandButton2.remove] etc...

:)
may I ask what you're to trying to do exactly?
 
Upvote 0
basically, at the moment i've created about 50 dynamic buttons in my userform. I'd like to clear them off the userform (not hide them) after the user click's one of the. The reason being that what ever button is clicked would create another whole set of dynamically created buttons in the same userform

So the problem is how do I target those created buttons for removal?
 
Upvote 0
Mike, will answer... me you waiting for it.
basically, at the moment i've created about 50 dynamic buttons in my userform. I'd like to clear them off the userform (not hide them) after the user click's one of the. The reason being that what ever button is clicked would create another whole set of dynamically created buttons in the same userform

So the problem is how do I target those created buttons for removal?

 
Upvote 0
Since you are adding command buttons, I'm guessing that they are getting their event code via a class module. Like this one (named Class1)
Code:
Public WithEvents RunTimeCommandButton As MSForms.CommandButton

Private Sub RunTimeCommandButton_Click()
    MsgBox RunTimeCommandButton.Name
End Sub

I'm further guessing that when you add the CommandButton's you are putting the newly generated instances of Class1 in a collection, as is done in butAddButtons_Click below.
The code for butRemoveRemoveControls_Click shows how to remove all the buttons represented in the AddedCommandButtons collection.

I made a test Userform with a frame (Frame1) and two command buttons(butAddButtons and butRemoveControls) and put this code into the userform's code module

Code:
Dim AddedCommandButtons As Collection

Private Sub butAddButtons_Click()
    Dim newCB As MSForms.CommandButton
    Dim addedCB As Class1
    Dim i As Long
    With Frame1
        For i = 1 To 50
            Set newCB = Frame1.Controls.Add("Forms.CommandButton.1", Visible:=True)
            With newCB
                .Height = 20: .Width = 100
                .Top = (.Height + 1) * i
                .Caption = .Name
            End With
            Set addedCB = New Class1
            Set addedCB.RunTimeCommandButton = newCB
            AddedCommandButtons.Add Item:=addedCB, Key:=newCB.Name
        Next i
    End With
End Sub

Private Sub butRemoveControls_Click()
    Dim oneAddedButton As Class1
    For Each oneAddedButton In AddedCommandButtons
        Me.Controls.Remove oneAddedButton.RunTimeCommandButton.Name
    Next oneAddedButton
    Set AddedCommandButtons = New Collection
End Sub

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

Private Sub UserForm_Terminate()
    Set AddedCommandButtons = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top