delete Userform control programmatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Have searched on discussion groups and not found what I am looking for.
How do you programmatically delete a Userform control. This is one that I
have created myself through VBA.
Ben
 
yes of course but i wish to do so while the Userform is running not while
designing it. Thank you however.
 
Geez, Ben, didn't turn out to be so easy to figure this out, but you got me
curious so I followed the trail and found the answer: You need to add the
VBIDE (Microsoft Visual Basic for Applications Extensibility) reference, and
then:

ThisWorkBook.VBProject.VBComponents.Remove _
ThisWorkBook.VBProject.VBComponents("UserForm1")
 
K. Dales,

I must apologize to everyone, it seems my question was not specific
enough. Let me try again. I want to delete a Control off of a Userform, not
the userform itself, and I wish to do so through VBA not in VBE in design
mode. I did find how to remove all controls, but I wish to do only one at a
time.
 
Assuming the control was added at runtime

Private Sub CommandButton1_Click()
me.Controls.Remove("textbox1")
End Sub
 
No, ben, I apologize as in retrospect you were clear that you were talking
about a userform control, not the userform itself. But, hey, I found out
something useful, right?

A run-time control can be deleted with .Remove, but the argument is the
index number in the controls collection, which can make it tricky since that
can change - but this seems to work:

i = 0
While i < UserForm1.Controls.Count
If UserForm1.Controls(i).Name = "MyControl" Then
UserForm1.Controls.Remove i
i = i + 1
Wend

Hope this finally gives you what you need!
 
Thank you, appreciate the assistance

Tom Ogilvy said:
Assuming the control was added at runtime

Private Sub CommandButton1_Click()
me.Controls.Remove("textbox1")
End Sub
 
This works fine (in a Userform):

Private Sub CommandButton1_Click()

Set ctrl = Me.Controls.Add _
(bstrprogID:="Forms.TextBox.1", _
Name:="TextBox1", Visible:=True)
ctrl.Top = Me.Controls("combobox1").Top + _
Me.Controls("Combobox1").Height + 10
ctrl.Left = Me.Controls("Combobox1").Left


End Sub

Private Sub CommandButton2_Click()
Me.Controls.Remove "Textbox1
End Sub

So it isn't restricted to a numerical index - while not real clear, the help
does say that it is not restricted to a numerical index.
 
Back
Top