Quick way to ClearContents of UserForm TextBoxes?

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
I have several UserForms with many TextBoxes.
To quickly ClearContents I've tried this which produces Object required error.
Any suggestions?

Code:
For i = 1 To TextBox.Count
         TextBox(i).ClearContents
        Next I
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You have to loop through all the controls and check the type:

Code:
For Each ctl in me.controls
    If LCase$(Typename(ctl)) = "textbox" then ctl.Text = vbnullstring
        Next ctl
 
Upvote 0
Thanks RoryA.
If the UserForm also includes ComboBoxes and Labels, can those controls be cleared with the same For each? (I've tried, unsuccessfully)
 
Upvote 0
Yes:

Code:
For Each ctl in me.controls
    Select Case LCase$(Typename(ctl))
        Case "textbox"
            ctl.Text = vbnullstring
        Case "combobox"
             ctl.Listindex = -1
        Case "label"
             ctl.Caption = vbnullstring
    End Select
        Next ctl

though generally you wouldn't need to clear labels.
 
Upvote 0
I’ve applied your code to one procedure, and it works perfectly.

Since I have many other Userforms that can use the same “upgrade”, I thought about creating a ClearTextBoxes Module.
Instead of replacing all the TextBox# = “” routines for each instance, I could simply:
Call MacroClearTextBoxes.

So, how do I set the reference for the UserForm? And, overcome the Compile error “Invalid use of Me Keyword?

Here’s my lame attempt:
Code:
Sub MacroClearTextBoxes()
With ActiveUserForm
 
For Each ctl In Me.Controls
    Select Case LCase$(TypeName(ctl))
        Case "textbox"
            ctl.Text = vbNullString
        Case "combobox"
             ctl.ListIndex = -1
    End Select
        Next ctl
End With
End Sub
 
Upvote 0
Pass the form as an object argument:

Code:
Sub MacroClearTextBoxes(uf as Userform)
Dim ctl as MSForms.Control
For Each ctl In uf.Controls
    Select Case LCase$(TypeName(ctl))
        Case "textbox"
            ctl.Text = vbNullString
        Case "combobox"
             ctl.ListIndex = -1
    End Select
    Next ctl
End Sub
 
Upvote 0
Rory,
I copied/pasted your code to my ClearTextBoxes module...
And changed procedure code in my UserForm CommandButton3_Click() to
Call MacroClearTextBoxes.

resulting in Compile error: "Argument not optional"

So...now I'm still lame :) and don't know how to fix it...in over my head.
 
Upvote 0
Code:
Call MacroClearTextBoxes(me)

is what you need.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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