Set value of all Userform textboxes to ""

G

Gig

I would like to set the value of all textboxes in a Userform to
"nothing" on an event. I can do this the long way by writing a line of
code for each textbox, but I'm thinking there is a faster/better way
by using "For Each - Next construct. I've got the following code, but
keep getting error messages. Can anyone tell me what's wrong with
this?

Private Sub CommandButton2_Click()

For Each TextBox In UserForm1
Value = ""
Next TextBox

End Sub

Thanks,
Greg
 
R

ryguy7272

Sooooo, close. You have to Dim it, to create a 'variable' for all those
TextBoxes.

Sub ClearCheckBoxes()
Dim ChkBox As Object

For Each ChkBox In ActiveSheet.CheckBoxes
ChkBox.Value = xlOff
Next ChkBox

End Sub

HTH,
Ryan---
 
R

ryguy7272

Whoops, sorry, try this:
Private Sub cmdNew_Click()
Dim C As MSForms.Control
For Each C In Me.Controls
If TypeOf C Is MSForms.TextBox Then
C.Text = ""
End If
Next C
End Sub

That is for TextBoxes, not ComboBoxes!!
Name the button cmdNew.

HTH,
Ryan---
 
G

gig4g4b

Whoops, sorry, try this:
Private Sub cmdNew_Click()
    Dim C As MSForms.Control
    For Each C In Me.Controls
    If TypeOf C Is MSForms.TextBox Then
    C.Text = ""
    End If
    Next C
End Sub

That is for TextBoxes, not ComboBoxes!!
Name the button cmdNew.

HTH,
Ryan---

Ryan, thanks for your help! That works perfectly!

Greg
 
R

ryguy7272

Great! Won, can you click ‘this post was helpful’, so others know the code
works.

Thanks,
Ryan---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top