Removing TextBoxes created at runtime

Pianoman23

New Member
Joined
May 16, 2014
Messages
25
Can anyone help? I'm using the following code to create and populate textboxes on a userform:
Code:
Dim ct As Integer
Dim txtB1 As Control
Dim lbl1 As Control
Dim r As Integer 'Database Row
Dim c As Integer 'Database Column
Dim t As Integer 'Top Position on screen
Dim l As Integer 'Left Position on screen
ct = Sheets("Database").Range("C18") 'Counts number of Branches on Database
'APRIL
'Create Textboxes
r = 20
c = 4
t = 84
l = 87
For i = 1 To ct
Set txtB1 = Controls.Add("Forms.TextBox.1")
    With txtB1
   .Name = "chkDemo" & i
   .Text = Sheets("database").Cells(r, c)
   .Height = 18
   .Width = 50
   .Left = l
   .Top = t
End With
c = c + 4
t = t + 20
Next i
Set txtB1 = Controls.Add("Forms.TextBox.1")
    With txtB1
   .Name = "chkDemo" & i
   .Text = Sheets("Summary").Range("D2")
   .Height = 18
   .Width = 50
   .Left = l
   .Top = t + 30
End With
Set txtB1 = Controls.Add("Forms.TextBox.1")
    With txtB1
   .Name = "chkDemo" & i
   .Text = Sheets("Summary").Range("B2")
   .Height = 18
   .Width = 50
   .Left = l
   .Top = t + 50
End With
Set txtB1 = Controls.Add("Forms.TextBox.1")
    With txtB1
   .Name = "chkDemo" & i
   .Text = Sheets("Summary").Range("F2")
   .Height = 18
   .Width = 50
   .Left = l
   .Top = t + 70
End With
Set txtB1 = Controls.Add("Forms.TextBox.1")
    With txtB1
   .Name = "chkDemo" & i
   .Text = Format(Sheets("Summary").Range("I2"), "Percent")
   .Height = 18
   .Width = 50
   .Left = l
   .Top = t + 90
End With
tbYTDa.Value = Worksheets("Summary").Range("H2")
tbYTDl.Value = Worksheets("Summary").Range("G2")
tbYTDpercent.Value = Format(Worksheets("Summary").Range("M2"), "Percent")
End If
This routine is repeated for the months of May, June, July, and so on.

Is there a code that will remove/delete all of the textbox controls previously created in one go?
 
Last edited by a moderator:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try...

Code:
    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "TextBox" Then
            Me.Controls.Remove Ctrl.Name
            'Ctrl.Visible = False
        End If
    Next Ctrl

Another option might be to hide them by setting the Visible property to False.

Hope this helps!
 
Upvote 0
Try...

Code:
    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "TextBox" Then
            Me.Controls.Remove Ctrl.Name
            'Ctrl.Visible = False
        End If
    Next Ctrl

Another option might be to hide them by setting the Visible property to False.

Hope this helps!

Thanks for your help - all works fine now.
 
Upvote 0

Forum statistics

Threads
1,216,771
Messages
6,132,611
Members
449,740
Latest member
tinkdrummer

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