VBA to remove control

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
I’ve built a userform. The userform contains “commandbutton1”, created at design time.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
A checkbox appears at runtime. The idea is that the checkbox is deleted when the user presses the command button.
<o:p> </o:p>
The code doesn’t work. Any ideas why?
<o:p> </o:p>
Code:
Private Sub Userform_initialize()<o:p></o:p>
<o:p> </o:p>
Set g = Me.Controls.Add("Forms.Checkbox.1")<o:p></o:p>
<o:p> </o:p>
g.Name = "CheckBox1"<o:p></o:p>
g.Caption = "Hello"<o:p></o:p>
g.Height = 22<o:p></o:p>
g.Left = 6<o:p></o:p>
g.Top = 12<o:p></o:p>
g.Width = 72<o:p></o:p>
g.Visible = True<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
Private Sub CommandButton1_Click()<o:p></o:p>
<o:p> </o:p>
UserForm1.Controls("CheckBox1").Remove<o:p></o:p>
<o:p> </o:p>
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I’ve built a userform. The userform contains “commandbutton1”, created at design time.
<o:p> </o:p>
A checkbox appears at runtime. The idea is that the checkbox is deleted when the user presses the command button.
<o:p> </o:p>
The code doesn’t work. Any ideas why?
<o:p> </o:p>
Code:
Private Sub Userform_initialize()<o:p></o:p>
<o:p> </o:p>
Set g = Me.Controls.Add("Forms.Checkbox.1")<o:p></o:p>
<o:p> </o:p>
g.Name = "CheckBox1"<o:p></o:p>
g.Caption = "Hello"<o:p></o:p>
g.Height = 22<o:p></o:p>
g.Left = 6<o:p></o:p>
g.Top = 12<o:p></o:p>
g.Width = 72<o:p></o:p>
g.Visible = True<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
Private Sub CommandButton1_Click()<o:p></o:p>
<o:p> </o:p>
UserForm1.Controls("CheckBox1").Remove<o:p></o:p>
<o:p> </o:p>
End Sub
Try:
Code:
Private Sub CommandButton1_Click()
UserForm1.Controls("Checkbox1").Visible = False
End Sub
 
Upvote 0
Try:
Rich (BB code):

Private Sub CommandButton1_Click()
  Controls.Remove "MyCheckBox1"
End Sub

Private Sub Userform_initialize()
  With Controls.Add("Forms.Checkbox.1")
    .Name = "MyCheckBox1"
    .Caption = "Hello"
    .Height = 22
    .Left = 6
    .Top = 12
    .Width = 72
    .Visible = True
  End With
End Sub
 
Upvote 0
Thanks for the reply, JoeMo.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
ZVI, that’s exactly what I was looking for.
 
Upvote 0
Try:
Rich (BB code):

Private Sub CommandButton1_Click()
  Controls.Remove "MyCheckBox1"
End Sub

Private Sub Userform_initialize()
  With Controls.Add("Forms.Checkbox.1")
    .Name = "MyCheckBox1"
    .Caption = "Hello"
    .Height = 22
    .Left = 6
    .Top = 12
    .Width = 72
    .Visible = True
  End With
End Sub


I'm not able to remove the checkbox using below:
Controls.Remove "MyCheckBox1"
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,634
Members
452,934
Latest member
Jdsonne31

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